jdk/test/sun/security/ssl/SSLSocketImpl/NotifyHandshakeTest.java
author xuelei
Wed, 05 Mar 2014 07:24:34 +0000
changeset 23052 241885315119
parent 5506 jdk/test/sun/security/ssl/com/sun/net/ssl/internal/ssl/SSLSocketImpl/NotifyHandshakeTest.java@202f599c92aa
child 23352 0b3d4e01b3c4
permissions -rw-r--r--
8032473: Restructure JSSE regression test hierarchy in jdk test Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2005, 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
 * This class may have some race conditions that I haven't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * accounted for.  I've tried to put in sufficient sleeps and triggers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * that should cause everything to run correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This was hackish, but to make sure that there were no problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * with permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Create a client, server, and interested party thread.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * client and interested threads should receive the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * session notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package com;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class NotifyHandshakeTest implements HandshakeCompletedListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 5506
diff changeset
    46
    static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    volatile static int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public boolean set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    SSLSession sess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static public int triggerState = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static public void trigger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        triggerState++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void handshakeCompleted(HandshakeCompletedEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        set = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        sess = event.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        trigger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        SSLSocketFactory sslsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * Start off the Server, give time to initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        SSLServerSocket sslss =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            (SSLServerSocket)sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        serverPort = sslss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Server server = new Server(sslss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("Server started...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
         * Create the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        SSLSocket socket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            (SSLSocket)sslsf.createSocket("localhost", serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * Create a second thread also interested in this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        edu.NotifyHandshakeTestHeyYou heyYou =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            new edu.NotifyHandshakeTestHeyYou(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        heyYou.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        while (triggerState < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println("HeyYou thread ready...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        NotifyHandshakeTest listener = new NotifyHandshakeTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        socket.addHandshakeCompletedListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.out.println("Client starting handshake...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        socket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        System.out.println("Client done handshaking...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        InputStream is = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if ((byte)is.read() != (byte)0x77) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new Exception("problem reading byte");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Wait for HeyYou and the client to get a slice, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * they can receive their SSLSessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        while (triggerState < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * Grab the variables before reaping the thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        boolean heyYouSet = heyYou.set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        AccessControlContext heyYouACC = heyYou.acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        SSLSession  heyYouSess = heyYou.ssls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        heyYou.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        heyYou.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        server.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!heyYouSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new Exception("HeyYou's wasn't set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (!listener.set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new Exception("This' wasn't set");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (heyYouACC.equals(AccessController.getContext())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new Exception("Access Control Contexts were the same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (!heyYouSess.equals(listener.sess)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new Exception("SSLSessions were not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        System.out.println("Everything Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SSLServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Server(SSLServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                System.out.println("Server accepting socket...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                SSLSocket s = (SSLSocket) ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    "Server accepted socket...starting handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                s.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                System.out.println("Server done handshaking");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                OutputStream os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                os.write(0x77);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                os.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                System.out.println("Server returning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                System.out.println("Server died");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}