jdk/test/javax/net/ssl/TLS/JSSEServer.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 39140 86f21a96d0ab
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
     1
/*
38430
e8d1995c38fa 8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use
asmotrak
parents: 26337
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
     4
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     7
 * published by the Free Software Foundation.
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
     8
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    13
 * accompanied this code).
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    14
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    18
 *
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 39140
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    21
 * questions.
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    22
 */
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    23
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    24
import java.io.IOException;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    25
import java.io.InputStream;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    26
import java.io.OutputStream;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    27
import javax.net.ssl.KeyManager;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    28
import javax.net.ssl.SSLContext;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    29
import javax.net.ssl.SSLServerSocket;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    30
import javax.net.ssl.SSLServerSocketFactory;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    31
import javax.net.ssl.SSLSocket;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    32
import javax.net.ssl.TrustManager;
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    33
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    34
public class JSSEServer extends CipherTestUtils.Server {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    35
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    36
    private final SSLServerSocket serverSocket;
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    37
    private static volatile boolean closeServer = false;
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    38
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    39
    JSSEServer(CipherTestUtils cipherTest, int serverPort,
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    40
            String protocol, String cipherSuite) throws Exception {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    41
        super(cipherTest);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    42
        SSLContext serverContext = SSLContext.getInstance("TLS");
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    43
        serverContext.init(new KeyManager[]{cipherTest.getServerKeyManager()},
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    44
                new TrustManager[]{cipherTest.getServerTrustManager()},
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    45
                CipherTestUtils.secureRandom);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    46
        SSLServerSocketFactory factory =
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    47
                (SSLServerSocketFactory)serverContext.getServerSocketFactory();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    48
        serverSocket =
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    49
                (SSLServerSocket) factory.createServerSocket(serverPort);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    50
        serverSocket.setEnabledProtocols(protocol.split(","));
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    51
        serverSocket.setEnabledCipherSuites(cipherSuite.split(","));
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    52
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    53
        CipherTestUtils.printInfo(serverSocket);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    54
    }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    55
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    56
    @Override
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    57
    public void run() {
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    58
        System.out.println("JSSE Server listening on port " + getPort());
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    59
        while (!closeServer) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    60
            try (final SSLSocket socket = (SSLSocket) serverSocket.accept()) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    61
                socket.setSoTimeout(CipherTestUtils.TIMEOUT);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    62
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    63
                try (InputStream in = socket.getInputStream();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    64
                        OutputStream out = socket.getOutputStream()) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    65
                    handleRequest(in, out);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    66
                    out.flush();
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    67
                } catch (IOException e) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    68
                    CipherTestUtils.addFailure(e);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    69
                    System.out.println("Got IOException:");
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    70
                    e.printStackTrace(System.out);
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    71
                }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    72
            } catch (Exception e) {
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    73
                CipherTestUtils.addFailure(e);
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    74
                System.out.println("Exception:");
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    75
                e.printStackTrace(System.out);
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    76
            }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    77
        }
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    78
    }
38430
e8d1995c38fa 8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use
asmotrak
parents: 26337
diff changeset
    79
e8d1995c38fa 8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use
asmotrak
parents: 26337
diff changeset
    80
    int getPort() {
e8d1995c38fa 8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use
asmotrak
parents: 26337
diff changeset
    81
        return serverSocket.getLocalPort();
e8d1995c38fa 8134267: javax/net/ssl/TLS/TestJSSE.java fails intermittently with BindException: Address already in use
asmotrak
parents: 26337
diff changeset
    82
    }
39140
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    83
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    84
    @Override
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    85
    public void close() throws IOException {
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    86
        closeServer = true;
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    87
        if (serverSocket != null && !serverSocket.isClosed()) {
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    88
            serverSocket.close();
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    89
        }
86f21a96d0ab 8152745: javax/net/ssl/TLS/TestJSSE.java fails intermittently: Unsupported or unrecognized SSL message
asmotrak
parents: 38430
diff changeset
    90
    }
26337
39f3ee5364e5 8049429: Tests for java client server communications with various TLS/SSL combinations.
xuelei
parents:
diff changeset
    91
}