test/jdk/sun/net/www/protocol/https/NewImpl/ComHTTPSConnection.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 30820 jdk/test/sun/net/www/protocol/https/NewImpl/ComHTTPSConnection.java@0d4717a011d3
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29615
b0057b63b4e7 8074531: Remove javax.security.cert.X509Certificate usage in internal networking packages
juh
parents: 29097
diff changeset
     2
 * Copyright (c) 2001, 2015, 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 4474255
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Can no longer obtain a com.sun.net.ssl.HttpsURLConnection
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 29615
diff changeset
    28
 * @modules java.base/com.sun.net.ssl
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 29615
diff changeset
    29
 *          java.base/com.sun.net.ssl.internal.www.protocol.https
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 * @run main/othervm ComHTTPSConnection
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    31
 *
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    32
 *     SunJSSE does not support dynamic system properties, no way to re-use
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    33
 *     system properties in samevm/agentvm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Brad Wetmore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.*;
29615
b0057b63b4e7 8074531: Remove javax.security.cert.X509Certificate usage in internal networking packages
juh
parents: 29097
diff changeset
    39
import java.security.cert.Certificate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.net.ssl.HostnameVerifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.net.ssl.HttpsURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * See if we can obtain a com.sun.net.ssl.HttpsURLConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * and then play with it a bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class ComHTTPSConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Set the various variables needed for the tests, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * specify what tests to run on each side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Should we run the client or server in a separate thread?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Both sides can throw exceptions, but do you have a preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * as to which side should be the main thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static boolean separateServerThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 10328
diff changeset
    66
    static String pathToStores = "../../../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Is the server ready to serve?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    volatile static boolean serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Turn on SSL debugging?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * If the client or server is doing some kind of object creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * that the other side depends on, and that thread prematurely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * exits, you may experience a hang.  The test harness will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * terminate all hung threads after its timeout has expired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * currently 3 minutes by default, but you might try to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * smart about it....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the path to the file obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * parsing the HTML header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static String getPath(DataInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        String line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        String path = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // extract class from GET line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (line.startsWith("GET /")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            line = line.substring(5, line.length()-1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            int index = line.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                path = line.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // eat the rest of header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } while ((line.length() != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (path.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new IOException("Malformed Header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns an array of bytes containing the bytes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * the file represented by the argument <b>path</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * In our case, we just pretend to send something back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return the bytes for the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @exception FileNotFoundException if the file corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * to <b>path</b> could not be loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private byte[] getBytes(String path)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return "Hello world, I am here".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Define the server side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void doServerSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        SSLServerSocketFactory sslssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
          (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        SSLServerSocket sslServerSocket =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            (SSLServerSocket) sslssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        serverPort = sslServerSocket.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * Signal Client, we're ready for his connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        DataOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                new DataOutputStream(sslSocket.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             // get path to class file from header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             DataInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        new DataInputStream(sslSocket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
             String path = getPath(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
             // retrieve bytecodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
             byte[] bytecodes = getBytes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             // send bytecodes in response (assumes HTTP/1.0 or later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                out.writeBytes("HTTP/1.0 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                out.writeBytes("Content-Length: " + bytecodes.length + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                out.writeBytes("Content-Type: text/html\r\n\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                out.write(bytecodes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
             } catch (IOException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                ie.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
             e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
             // write out error response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
             out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
             out.writeBytes("Content-Type: text/html\r\n\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
             // close the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
             System.out.println("Server closing socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             sslSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             serverReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   193
    private static class ComSunHTTPSHandlerFactory implements URLStreamHandlerFactory {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   194
        private static String SUPPORTED_PROTOCOL = "https";
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   195
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   196
        public URLStreamHandler createURLStreamHandler(String protocol) {
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   197
            if (!protocol.equalsIgnoreCase(SUPPORTED_PROTOCOL))
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   198
                return null;
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   199
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   200
            return new com.sun.net.ssl.internal.www.protocol.https.Handler();
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   201
        }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   202
    }
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   203
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Define the client side of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * If the server prematurely exits, serverReady will be set to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * to avoid infinite hangs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * Wait for server to get started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        while (!serverReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   218
        HostnameVerifier reservedHV =
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   219
            HttpsURLConnection.getDefaultHostnameVerifier();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   220
        try {
29097
01a70c01f5a1 8064924: Update java.net.URL to work with modules
chegar
parents: 23052
diff changeset
   221
            URL.setURLStreamHandlerFactory(new ComSunHTTPSHandlerFactory());
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   222
            HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   224
            URL url = new URL("https://" + "localhost:" + serverPort +
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   225
                                    "/etc/hosts");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   226
            URLConnection urlc = url.openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   228
            if (!(urlc instanceof com.sun.net.ssl.HttpsURLConnection)) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   229
                throw new Exception(
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   230
                    "URLConnection ! instanceof " +
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   231
                    "com.sun.net.ssl.HttpsURLConnection");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   234
            BufferedReader in = null;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   235
            try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   236
                in = new BufferedReader(new InputStreamReader(
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   237
                                   urlc.getInputStream()));
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   238
                String inputLine;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   239
                System.out.print("Client reading... ");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   240
                while ((inputLine = in.readLine()) != null)
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   241
                    System.out.println(inputLine);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   242
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   243
                System.out.println("Cipher Suite: " +
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   244
                    ((HttpsURLConnection)urlc).getCipherSuite());
29615
b0057b63b4e7 8074531: Remove javax.security.cert.X509Certificate usage in internal networking packages
juh
parents: 29097
diff changeset
   245
                Certificate[] certs =
b0057b63b4e7 8074531: Remove javax.security.cert.X509Certificate usage in internal networking packages
juh
parents: 29097
diff changeset
   246
                    ((HttpsURLConnection)urlc).getServerCertificates();
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   247
                for (int i = 0; i < certs.length; i++) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   248
                    System.out.println(certs[0]);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   249
                }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   250
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                in.close();
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   252
            } catch (SSLException e) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   253
                if (in != null)
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   254
                    in.close();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   255
                throw e;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   256
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   257
            System.out.println("Client reports:  SUCCESS");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   258
        } finally {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   259
            HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        public boolean verify(String urlHostname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        String certHostname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                "CertificateHostnameVerifier: " + urlHostname + " == "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                + certHostname + " returning true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * =============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * The remainder is just support stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    // use any free port by default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    volatile int serverPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    volatile Exception serverException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    volatile Exception clientException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            System.setProperty("javax.net.debug", "all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         * Start the tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        new ComHTTPSConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    Thread clientThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    Thread serverThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Primary constructor, used to drive remainder of the test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Fork off the other side, then do your work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    ComHTTPSConnection() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            startServer(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            startClient(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            startClient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            startServer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * Wait for other side to close down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (separateServerThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            serverThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            clientThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * When we get here, the test is pretty much over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * If the main thread excepted, that propagates back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * immediately.  If the other thread threw an exception, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * should report back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (serverException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            System.out.print("Server Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw serverException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (clientException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            System.out.print("Client Exception:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw clientException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    void startServer(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            serverThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                         * Our server thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                         * Release the client, if not active already...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        System.err.println("Server died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        serverReady = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        serverException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            serverThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            doServerSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    void startClient(boolean newThread) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (newThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            clientThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                         * Our client thread just died.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        System.err.println("Client died...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        clientException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            clientThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}