jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java
author asmotrak
Tue, 14 Jun 2016 10:27:23 -0700
changeset 38952 ca428fb526d4
parent 36003 6519aa869709
permissions -rw-r--r--
8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34948
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
     2
 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
    24
//
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
    26
// system properties in samevm/agentvm mode.
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
    27
//
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @test
23743
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
    31
 * @bug 4366807
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary Need new APIs to get/set session timeout and session cache size.
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    33
 * @run main/othervm SessionCacheSizeTests
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Session cache size tests cover the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 1. Effect of system property javax.net.ssl.SessionCacheSize (this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * property is not documented for public).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 2. Reducing the cache size, results in uncaching of sessions if #of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * sessions present exceeds the new size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 3. Increasing the cache size, results in accomodating  new sessions if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * number of cached sessions is the current size limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Invairant for passing this test is, at any given time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * #cached_sessions <= current_cache_size , for current_cache_size > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class SessionCacheSizeTests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 19575
diff changeset
    73
    static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * A limit on the number of connections at any given time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static int MAX_ACTIVE_CONNECTIONS = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   109
    static final int FREE_PORT = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   111
    void doServerSide(int serverConns) throws Exception {
34948
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   112
        try (SSLServerSocket sslServerSocket =
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   113
                (SSLServerSocket) sslssf.createServerSocket(FREE_PORT)) {
34948
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   114
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   115
            // timeout to accept a connection
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   116
            sslServerSocket.setSoTimeout(45000);
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   117
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   118
            // make sure createdPorts++ is atomic
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   119
            synchronized(serverPorts) {
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   120
                int serverPort = sslServerSocket.getLocalPort();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   121
                System.out.printf("server #%d started on port %d%n",
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   122
                        createdPorts, serverPort);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   123
                serverPorts[createdPorts++] = serverPort;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
34948
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   125
                /*
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   126
                 * Signal Client, we're ready for his connect.
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   127
                 */
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   128
                if (createdPorts == serverPorts.length) {
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   129
                    serverReady = true;
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   130
                }
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   131
            }
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   132
            int read = 0;
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   133
            int nConnections = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
34948
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   135
            /*
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   136
             * Divide the max connections among the available server ports.
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   137
             * The use of more than one server port ensures creation of more
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   138
             * than one session.
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   139
             */
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   140
            SSLSession sessions [] = new SSLSession [serverConns];
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   141
            SSLSessionContext sessCtx = sslctx.getServerSessionContext();
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   142
23743
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   143
            while (nConnections < serverConns) {
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   144
                try (SSLSocket sslSocket =
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   145
                        (SSLSocket)sslServerSocket.accept()) {
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   146
                    sslSocket.setSoTimeout(90000);      // timeout to read
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   147
                    InputStream sslIS = sslSocket.getInputStream();
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   148
                    OutputStream sslOS = sslSocket.getOutputStream();
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   149
                    read = sslIS.read();
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   150
                    sessions[nConnections] = sslSocket.getSession();
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   151
                    sslOS.write(85);
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   152
                    sslOS.flush();
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   153
                    nConnections++;
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   154
                }
92cae64be74e 8037557: test SessionCacheSizeTests.java timeout
xuelei
parents: 23052
diff changeset
   155
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int nConnections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        SSLSocket sslSockets[] = new SSLSocket [MAX_ACTIVE_CONNECTIONS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        Vector sessions = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        SSLSessionContext sessCtx = sslctx.getClientSessionContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        sessCtx.setSessionTimeout(0); // no limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   180
        try {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   181
            while (nConnections < (MAX_ACTIVE_CONNECTIONS - 1)) {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   182
                // divide the connections among the available server ports
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   183
                int serverPort = serverPorts [nConnections % (serverPorts.length)];
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   184
                System.out.printf("client #%d connects to port %d%n",
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   185
                        nConnections, serverPort);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   186
                sslSockets[nConnections] = (SSLSocket) sslsf.
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   187
                            createSocket("localhost",
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   188
                            serverPort);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   189
                InputStream sslIS = sslSockets[nConnections].getInputStream();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   190
                OutputStream sslOS = sslSockets[nConnections].getOutputStream();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   191
                sslOS.write(237);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   192
                sslOS.flush();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   193
                int read = sslIS.read();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   194
                SSLSession sess = sslSockets[nConnections].getSession();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   195
                if (!sessions.contains(sess))
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   196
                    sessions.add(sess);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   197
                nConnections++;
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   198
            }
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   199
            System.out.println("Current cacheSize is set to: " +
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   200
                                    sessCtx.getSessionCacheSize());
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   201
            System.out.println();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   202
            System.out.println("Currently cached Sessions......");
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   203
            System.out.println("============================================"
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   204
                                    + "============================");
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   205
            System.out.println("Session                                     "
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   206
                                    + "      Session-last-accessTime");
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   207
            System.out.println("============================================"
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   208
                                    + "============================");
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   209
            checkCachedSessions(sessCtx, nConnections);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   210
            // Change session cache size
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   211
            sessCtx.setSessionCacheSize(2);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   212
            System.out.println("Session cache size changed to: "
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   213
                            + sessCtx.getSessionCacheSize());
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   214
            System.out.println();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   215
            checkCachedSessions(sessCtx, nConnections);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   216
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   217
            // Test the effect of increasing the cache size
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   218
            sessCtx.setSessionCacheSize(3);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   219
            System.out.println("Session cache size changed to: "
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   220
                            + sessCtx.getSessionCacheSize());
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   221
            // create a new session
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   222
            int serverPort = serverPorts [nConnections % (serverPorts.length)];
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   223
            System.out.printf("client #%d connects to port %d%n",
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   224
                    nConnections, serverPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            sslSockets[nConnections] = (SSLSocket) sslsf.
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   226
                            createSocket("localhost",
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   227
                            serverPort);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            InputStream sslIS = sslSockets[nConnections].getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            OutputStream sslOS = sslSockets[nConnections].getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            sslOS.write(237);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            sslOS.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            int read = sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            SSLSession sess = sslSockets[nConnections].getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (!sessions.contains(sess))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                sessions.add(sess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            nConnections++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   238
            // test the number of sessions cached against the cache size
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   239
            checkCachedSessions(sessCtx, nConnections);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   240
        } finally {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   241
            for (int i = 0; i < nConnections; i++) {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   242
                if (sslSockets[i] != null) {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   243
                    sslSockets[i].close();
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   244
                }
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   245
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        System.out.println("Session cache size tests passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    void checkCachedSessions(SSLSessionContext sessCtx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                int nConn) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int nSessions = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Enumeration e = sessCtx.getIds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int cacheSize = sessCtx.getSessionCacheSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        SSLSession sess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            sess = sessCtx.getSession((byte[]) e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            long lastAccessedTime  = sess.getLastAccessedTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                System.out.println(sess + "       "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        +  new Date(lastAccessedTime));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            nSessions++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.out.println("--------------------------------------------"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                + "----------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if ((cacheSize > 0) && (nSessions > cacheSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // close all active connections before exiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            for (int conn = nConn; conn < MAX_ACTIVE_CONNECTIONS; conn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                SSLSocket s = (SSLSocket) sslsf.createSocket("localhost",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                        serverPorts [conn % (serverPorts.length)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new Exception("Session cache size test failed,"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                + " current cache size: " + cacheSize + " #sessions cached: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                + nSessions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * #of ports > 1, guarantees creation of more than one session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Using four ports (one per each connection), we are able to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * alteast four sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
34948
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   291
    int serverPorts[] = new int[]{0, 0, 0, 0};  // MAX_ACTIVE_CONNECTIONS: 4
9293584191b6 8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
xuelei
parents: 34947
diff changeset
   292
    int createdPorts = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    static SSLServerSocketFactory sslssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    static SSLSocketFactory sslsf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static SSLContext sslctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // test the effect of javax.net.ssl.sessionCacheSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        System.setProperty("javax.net.ssl.sessionCacheSize", String.valueOf(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        sslctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        KeyStore ks = KeyStore.getInstance("JKS");
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   319
        try (FileInputStream fis = new FileInputStream(keyFilename)) {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   320
            ks.load(fis, passwd.toCharArray());
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   321
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        kmf.init(ks, passwd.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        sslctx.init(kmf.getKeyManagers(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        sslssf = (SSLServerSocketFactory) sslctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        sslsf = (SSLSocketFactory) sslctx.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        new SessionCacheSizeTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    SessionCacheSizeTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * create the SSLServerSocket and SSLSocket factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * Divide the max connections among the available server ports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * The use of more than one server port ensures creation of more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * than one session.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int serverConns = MAX_ACTIVE_CONNECTIONS / (serverPorts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int remainingConns = MAX_ACTIVE_CONNECTIONS % (serverPorts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   356
        Exception startException = null;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   357
        try {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   358
            if (separateServerThread) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   359
                for (int i = 0; i < serverPorts.length; i++) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   360
                    // distribute remaining connections among the
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   361
                    // available ports
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   362
                    if (i < remainingConns) {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   363
                        startServer(serverConns + 1, true);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   364
                    } else {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   365
                        startServer(serverConns, true);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   366
                    }
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   367
                }
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   368
                startClient(false);
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   369
            } else {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   370
                startClient(true);
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   371
                for (int i = 0; i < serverPorts.length; i++) {
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   372
                    if (i < remainingConns) {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   373
                        startServer(serverConns + 1, false);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   374
                    } else {
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   375
                        startServer(serverConns, false);
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   376
                    }
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   377
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   379
        } catch (Exception e) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   380
            startException = e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (separateServerThread) {
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   387
            if (serverThread != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   388
                serverThread.join();
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   389
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } else {
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   391
            if (clientThread != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   392
                clientThread.join();
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   393
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * When we get here, the test is pretty much over.
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   398
         */
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   399
        Exception local;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   400
        Exception remote;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   401
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   402
        if (separateServerThread) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   403
            remote = serverException;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   404
            local = clientException;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   405
        } else {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   406
            remote = clientException;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   407
            local = serverException;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   408
        }
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   409
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   410
        Exception exception = null;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   411
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   412
        /*
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   413
         * Check various exception conditions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         */
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   415
        if ((local != null) && (remote != null)) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   416
            // If both failed, return the curthread's exception.
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   417
            local.initCause(remote);
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   418
            exception = local;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   419
        } else if (local != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   420
            exception = local;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   421
        } else if (remote != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   422
            exception = remote;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   423
        } else if (startException != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   424
            exception = startException;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   425
        }
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   426
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   427
        /*
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   428
         * If there was an exception *AND* a startException,
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   429
         * output it.
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   430
         */
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   431
        if (exception != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   432
            if (exception != startException && startException != null) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   433
                exception.addSuppressed(startException);
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   434
            }
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   435
            throw exception;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   436
        }
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   437
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   438
        // Fall-through: no exception to throw!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   441
    void startServer(final int nConns, boolean newThread) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    try {
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   446
                        doServerSide(nConns);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        } else {
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   462
            try {
38952
ca428fb526d4 8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
asmotrak
parents: 36003
diff changeset
   463
                doServerSide(nConns);
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   464
            } catch (Exception e) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   465
                serverException = e;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   466
            } finally {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   467
                serverReady = true;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   468
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    void startClient(boolean newThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                 throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } else {
19575
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   490
            try {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   491
                doClientSide();
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   492
            } catch (Exception e) {
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   493
                clientException = e;
ff87fda24d33 8022228: Intermittent test failures in sun/security/ssl/javax/net/ssl/NewAPIs
xuelei
parents: 14342
diff changeset
   494
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}