test/jdk/sun/security/ssl/SSLSocketImpl/SetClientMode.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
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) 2005, 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
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    24
// SunJSSE does not support dynamic system properties, no way to re-use
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    25
// system properties in samevm/agentvm mode.
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @bug 6223624
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    30
 * @ignore this test does not grant to work.  The handshake may have completed
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 47216
diff changeset
    31
 *        when getSession() return.  Please update or remove this test case.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary SSLSocket.setUseClientMode() fails to throw expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *        IllegalArgumentException
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    34
 * @run main/othervm SetClientMode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Attempts to replicate a TCK test failure which creates SSLServerSockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and then runs client threads which connect and start handshaking. Once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * handshaking is begun the server side attempts to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * SSLSocket.setUseClientMode() on one or the other of the ends of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * connection, expecting an IllegalArgumentException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * If the server side of the connection tries setUseClientMode() we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * see the expected exception. If the setting is tried on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * client side SSLSocket, we do *not* see the exception, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * occasionally on the very first iteration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class SetClientMode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static String[] algorithms = {"TLS", "SSL", "SSLv3", "TLS"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 14342
diff changeset
    64
    static String pathToStores = "../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public SetClientMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // trivial constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        new SetClientMode().run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        for (int i = 0; i < algorithms.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            testCombo( algorithms[i] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void testCombo(String algorithm) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Exception modeException = null ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // Create a server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        SSLServerSocketFactory ssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        SSLServerSocket serverSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            (SSLServerSocket)ssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        serverPort = serverSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Create a client socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SSLSocket clientSocket = (SSLSocket)sf.createSocket(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                InetAddress.getLocalHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                serverPort );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // Create a client which will use the SSLSocket to talk to the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SocketClient client = new SocketClient(clientSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Start the client and then accept any connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        client.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        SSLSocket connectedSocket = (SSLSocket)serverSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // force handshaking to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        connectedSocket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // Now try invoking setClientMode() on one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // or the other of our two sockets. We expect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // to see an IllegalArgumentException because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // handshaking has begun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            clientSocket.setUseClientMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            modeException = new Exception("no IllegalArgumentException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.out.println("succeeded, we can't set the client mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            modeException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // Shut down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            connectedSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (modeException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                throw modeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // A thread-based client which does nothing except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // start handshaking on the socket it's given.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    class SocketClient extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        SSLSocket clientsideSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public SocketClient( SSLSocket s ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            clientsideSocket = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                clientsideSocket.startHandshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                // If we were to invoke setUseClientMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                // here, the expected exception will happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                //clientsideSocket.getSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                //clientsideSocket.setUseClientMode( false );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } catch ( Exception e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    clientsideSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    // eat it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        boolean isDone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Exception getException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}