jdk/test/javax/net/ssl/SSLSession/HttpsURLConnectionLocalCertificateChain.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 23052 241885315119
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10328
diff changeset
     2
 * Copyright (c) 2001, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4395238 4354003 4387961 4395266
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary A test of many of the new functionality to go into JSSE 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *      Fixed 4395238: The new certificate chains APIs should really be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          returning certs, not x509 certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *      Fixed 4354003: Need API to get client certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *      Fixed 4387961: HostnameVerifier needs to pass various hostnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      Fixed 4395266: HttpsURLConnection should be made protected
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    33
 * @run main/othervm HttpsURLConnectionLocalCertificateChain
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    34
 *
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    35
 *     SunJSSE does not support dynamic system properties, no way to re-use
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    36
 *     system properties in samevm/agentvm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class HttpsURLConnectionLocalCertificateChain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        implements HandshakeCompletedListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static boolean separateServerThread = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    65
    static String pathToStores = "../etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Default Verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            Certificate [] certs = session.getPeerCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            for (int i = 0; i< certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                if (certs[i] instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    System.out.println("Hostname Verification cert #1: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    // System.out.println(certs[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The event sent by the app.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    HandshakeCompletedEvent event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Provide the Listener for the HandshakeCompletedEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Store the event now, we'll examine it later as we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * finishing the test...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public void handshakeCompleted(HandshakeCompletedEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        event = theEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    void examineHandshakeCompletedEvent() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         * Also check the types during compile.  We changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         * from cert.x509 to certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        dumpCerts("examineHandshakeCompletedEvent received",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            event.getPeerCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        dumpCerts("examineHandshakeCompletedEvent sent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            event.getLocalCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    synchronized void dumpCerts(String where, Certificate [] certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println(where + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new Exception("certs == null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i = 0; i< certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (certs[i] instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                System.out.println("cert #1: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    ((X509Certificate) certs[i]).getSubjectDN());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        SSLServerSocketFactory sslssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        SSLServerSocket sslServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        System.out.println("Starting Server...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        System.out.println("Kicking off Client...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        sslSocket.setNeedClientAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        sslSocket.addHandshakeCompletedListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        InputStream sslIS = sslSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        OutputStream sslOS = sslSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        DataOutputStream out = new DataOutputStream(sslOS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        System.out.println("Server reading request...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.out.println("Server replying...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            out.writeBytes("HTTP/1.0 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out.writeBytes("Content-Length: " + 1 + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            out.writeBytes("Content-Type: text/html\r\n\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            out.write(57);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } catch (IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            serverException = ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.out.println("Server getting certs...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        SSLSession sslSession = sslSocket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        dumpCerts("ServerSide sent", sslSession.getLocalCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        dumpCerts("ServerSide received", sslSession.getPeerCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * Won't bother closing IS/sockets this time, we're exiting...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * We'll eventually get this event, wait for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        while (event == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        System.out.println("Server examining Event...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        examineHandshakeCompletedEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        System.out.println("Starting Client...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String url = "https://localhost:" + serverPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        System.out.println("connecting to: " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        URL myURL = new URL(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        HttpsURLConnection myURLc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        System.out.println("Client setting up URL/connecting...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        myURLc = (HttpsURLConnection) myURL.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        myURLc.setHostnameVerifier(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        myURLc.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        InputStream sslIS = myURLc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        System.out.println("Client reading...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        sslIS.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        System.out.println("Client dumping certs...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        dumpCerts("ClientSide received", myURLc.getServerCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        dumpCerts("ClientSide sent", myURLc.getLocalCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
         * Won't bother closing IS/sockets this time, we're exiting...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        new HttpsURLConnectionLocalCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    HttpsURLConnectionLocalCertificateChain () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            clientThread.join();
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
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (serverException != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (clientException != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        System.out.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        System.out.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}