test/jdk/sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java
author dfuchs
Thu, 12 Sep 2019 15:46:11 +0100
changeset 58108 5302477c8285
parent 47216 71c04702a3d5
permissions -rw-r--r--
8230858: Replace wildcard address with loopback or local host in tests - part 23 Summary: Add new traces for better diagnosis, refrain binding to the wildcard address when possible. Reviewed-by: chegar, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58108
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, 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
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    24
//
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    25
// SunJSSE does not support dynamic system properties, no way to re-use
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    26
// system properties in samevm/agentvm mode.
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    27
//
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    28
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @bug 4328195
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary Need to include the alternate subject DN for certs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *          https should check for this
41196
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    34
 * @library /javax/net/ssl/templates
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    35
 * @run main/othervm ServerIdentityTest dnsstore localhost
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    36
 * @run main/othervm ServerIdentityTest ipstore 127.0.0.1
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    37
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Yingxian Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    41
import java.io.InputStream;
41196
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    42
import java.io.BufferedWriter;
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    43
import java.io.OutputStreamWriter;
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    44
import java.net.HttpURLConnection;
58108
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    45
import java.net.InetAddress;
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    46
import java.net.Proxy;
41196
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    47
import java.net.URL;
58108
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    48
import java.net.UnknownHostException;
42668
9518a1858a96 8171043: ServerIdentityTest.java fails on Windows
mli
parents: 42466
diff changeset
    49
41196
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    50
import javax.net.ssl.HttpsURLConnection;
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
    51
import javax.net.ssl.SSLContext;
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    52
import javax.net.ssl.SSLSocket;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    53
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    54
public final class ServerIdentityTest extends SSLSocketTemplate {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    56
    private static String keystore;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    57
    private static String hostname;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    58
    private static SSLContext context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    60
    /*
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    61
     * Run the test case.
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    62
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void main(String[] args) throws Exception {
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    64
        // Get the customized arguments.
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    65
        initialize(args);
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    66
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    67
        (new ServerIdentityTest()).run();
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    68
    }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    69
58108
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    70
    ServerIdentityTest() throws UnknownHostException {
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    71
        serverAddress = InetAddress.getByName(hostname);
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    72
    }
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    73
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    74
    @Override
42466
321b4a747cec 8171003: A couple of JSSE tests have been failing after JDK-8170329
xuelei
parents: 42464
diff changeset
    75
    protected boolean isCustomizedClientConnection() {
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    76
        return true;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    77
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    79
    @Override
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    80
    protected void runServerApplication(SSLSocket socket) throws Exception {
42668
9518a1858a96 8171043: ServerIdentityTest.java fails on Windows
mli
parents: 42466
diff changeset
    81
        InputStream sslIS = socket.getInputStream();
9518a1858a96 8171043: ServerIdentityTest.java fails on Windows
mli
parents: 42466
diff changeset
    82
        sslIS.read();
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    83
        BufferedWriter bw = new BufferedWriter(
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    84
                new OutputStreamWriter(socket.getOutputStream()));
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    85
        bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    86
        bw.flush();
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    87
        socket.getSession().invalidate();
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    88
    }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    89
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    90
    @Override
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    91
    protected void runClientApplication(int serverPort) throws Exception {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    92
        URL url = new URL(
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    93
                "https://" + hostname + ":" + serverPort + "/index.html");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    95
        HttpURLConnection urlc = null;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    96
        InputStream is = null;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    97
        try {
58108
5302477c8285 8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents: 47216
diff changeset
    98
            urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
    99
            is = urlc.getInputStream();
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   100
        } finally {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   101
            if (is != null) {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   102
                is.close();
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   103
            }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   104
            if (urlc != null) {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   105
                urlc.disconnect();
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   106
            }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   107
        }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   108
    }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   109
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   110
    @Override
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   111
    protected SSLContext createServerSSLContext() throws Exception {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   112
        return context;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   113
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
42464
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   115
    @Override
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   116
    protected SSLContext createClientSSLContext() throws Exception {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   117
        return context;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   118
    }
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   119
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   120
    private static void initialize(String[] args) throws Exception {
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   121
        keystore = args[0];
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   122
        hostname = args[1];
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   123
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   124
        String password = "changeit";
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   125
        String keyFilename =
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   126
                System.getProperty("test.src", ".") + "/" + keystore;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   127
        String trustFilename =
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   128
                System.getProperty("test.src", ".") + "/" + keystore;
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   129
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   130
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   131
        System.setProperty("javax.net.ssl.keyStorePassword", password);
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   132
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   133
        System.setProperty("javax.net.ssl.trustStorePassword", password);
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   134
38d967704a9f 8170329: New SSLSocket testing template
xuelei
parents: 42155
diff changeset
   135
        context = SSLContext.getDefault();
41196
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
   136
        HttpsURLConnection.setDefaultSSLSocketFactory(
240314163e51 8164591: sun/net/www/protocol/https/HttpsClient/ServerIdentityTest.java failed with SSLHandshakeException
asmotrak
parents: 23052
diff changeset
   137
                context.getSocketFactory());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}