jdk/test/sun/net/www/protocol/https/HttpsClient/ProxyAuthTest.java
author mchung
Thu, 28 May 2015 10:54:48 -0700
changeset 30820 0d4717a011d3
parent 23052 241885315119
child 41579 c0fe2e6364d9
permissions -rw-r--r--
8081347: Add @modules to jdk_core tests Reviewed-by: alanb, joehw, lancea Contributed-by: alexander.kulyakhtin@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 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
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 4323990 4413069
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HttpsURLConnection doesn't send Proxy-Authorization on CONNECT
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    28
 *     Incorrect checking of proxy server response
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 23052
diff changeset
    29
 * @modules java.base/sun.net.www
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    30
 * @run main/othervm ProxyAuthTest
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
 *     No way to reserve and restore java.lang.Authenticator, need to run this
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    33
 *     test in othervm mode.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ProxyAuthTest.java -- includes a simple server that can serve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Http get request in both clear and secure channel, and a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * that makes https requests behind the firewall through an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * authentication proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class ProxyAuthTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Where do we find the keystores?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
23052
241885315119 8032473: Restructure JSSE regression test hierarchy in jdk test
xuelei
parents: 10328
diff changeset
    54
    static String pathToStores = "../../../../../../javax/net/ssl/etc";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static String keyStoreFile = "keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static String trustStoreFile = "truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static String passwd = "passphrase";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    volatile private static 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
     * The TestServer implements a OriginServer that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * processes HTTP requests and responses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static class TestServer extends OriginServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public TestServer(ServerSocket ss) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            super(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * Returns an array of bytes containing the bytes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * the data sent in the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * @return bytes for the data in the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return "Proxy authentication for tunneling succeeded ..".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Main method to create the server and the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    85
    public static void main(String args[]) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String keyFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                "/" + keyStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        String trustFilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.getProperty("test.src", "./") + "/" + pathToStores +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "/" + trustStoreFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.setProperty("javax.net.ssl.keyStore", keyFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.setProperty("javax.net.ssl.trustStore", trustFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.setProperty("javax.net.ssl.trustStorePassword", passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        boolean useSSL = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * setup the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ServerSocketFactory ssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                ProxyAuthTest.getServerSocketFactory(useSSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            ServerSocket ss = ssf.createServerSocket(serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            serverPort = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            new TestServer(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.out.println("Server side failed:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // trigger the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            doClientSide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (Exception e) {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   117
            System.out.println("Client side failed: " + e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw e;
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   119
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static ServerSocketFactory getServerSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                   (boolean useSSL) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (useSSL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            SSLServerSocketFactory ssf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // set up key manager to do server authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            SSLContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            KeyManagerFactory kmf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            char[] passphrase = passwd.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ctx = SSLContext.getInstance("TLS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            kmf = KeyManagerFactory.getInstance("SunX509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ks.load(new FileInputStream(System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        "javax.net.ssl.keyStore")), passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            kmf.init(ks, passphrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            ctx.init(kmf.getKeyManagers(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            ssf = ctx.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return ssf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return ServerSocketFactory.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static void doClientSide() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * setup up a proxy with authentication information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        setupProxy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * we want to avoid URLspoofCheck failures in cases where the cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * DN name does not match the hostname in the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        HttpsURLConnection.setDefaultHostnameVerifier(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                      new NameVerifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        URL url = new URL("https://" + "localhost:" + serverPort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                + "/index.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        BufferedReader in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            in = new BufferedReader(new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                               url.openStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            String inputLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.out.print("Client recieved from the server: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            while ((inputLine = in.readLine()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                System.out.println(inputLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } catch (SSLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (in != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static class NameVerifier implements HostnameVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public boolean verify(String hostname, SSLSession session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static void setupProxy() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ProxyTunnelServer pserver = new ProxyTunnelServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
         * register a system wide authenticator and setup the proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         * authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Authenticator.setDefault(new TestAuthenticator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // register with the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        pserver.needUserAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        pserver.setUserAuth("Test", "test123");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        pserver.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        System.setProperty("https.proxyHost", "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        System.setProperty("https.proxyPort", String.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                        pserver.getPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static class TestAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        public PasswordAuthentication getPasswordAuthentication() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return new PasswordAuthentication("Test",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                         "test123".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}