jdk/test/javax/net/ssl/HttpsURLConnection/Equals.java
author ssahoo
Thu, 07 Jul 2016 09:50:35 -0700
changeset 39493 837860f3f432
parent 38327 77143af4d719
permissions -rw-r--r--
8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210 Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     1
/*
38327
77143af4d719 8155088: Fix module dependencies in java/sql/* and javax/* tests
xiaofeya
parents: 26221
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     4
 *
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     8
 *
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    13
 * accompanied this code).
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    14
 *
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    18
 *
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    21
 * questions.
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    22
 */
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    23
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    24
/**
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    25
 * @test
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    26
 * @bug 8055299
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    27
 * @library /lib/testlibrary
38327
77143af4d719 8155088: Fix module dependencies in java/sql/* and javax/* tests
xiaofeya
parents: 26221
diff changeset
    28
 * @modules jdk.httpserver
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    29
 * @build jdk.testlibrary.SimpleSSLContext
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    30
 * @run main/othervm -Djavax.net.debug=ssl,handshake,record Equals
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    31
 */
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    32
import com.sun.net.httpserver.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    33
import java.net.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    34
import java.io.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    35
import javax.net.ssl.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    36
import java.util.concurrent.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    37
import jdk.testlibrary.SimpleSSLContext;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    38
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    39
public class Equals {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    40
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    41
    static SSLContext ctx;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    42
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    43
    public static void main(String[] args) throws Exception {
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    44
        HttpsServer s2 = null;
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    45
        ExecutorService executor = null;
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    46
        try {
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    47
            InetSocketAddress addr = new InetSocketAddress(0);
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    48
            s2 = HttpsServer.create(addr, 0);
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    49
            HttpHandler h = new Handler();
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    50
            HttpContext c2 = s2.createContext("/test1", h);
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    51
            executor = Executors.newCachedThreadPool();
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    52
            s2.setExecutor(executor);
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    53
            ctx = new SimpleSSLContext().get();
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    54
            s2.setHttpsConfigurator(new HttpsConfigurator(ctx));
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    55
            s2.start();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    56
            int httpsport = s2.getAddress().getPort();
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    57
            System.out.printf("%nServer address: %s%n", s2.getAddress());
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    58
            test(httpsport);
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    59
            System.out.println("OK");
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    60
        } finally {
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    61
            if (s2 != null) {
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    62
                s2.stop(2);
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    63
            }
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    64
            if (executor != null) {
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    65
                executor.shutdown();
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    66
            }
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    67
        }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    68
    }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    69
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    70
    static class Handler implements HttpHandler {
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    71
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    72
        int invocation = 1;
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    73
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    74
        public void handle(HttpExchange t)
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    75
                throws IOException {
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    76
            InputStream is = t.getRequestBody();
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    77
            while (is.read() != -1) {
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    78
            }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    79
            is.close();
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    80
            t.sendResponseHeaders(200, 0);
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    81
            t.close();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    82
        }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    83
    }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    84
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    85
    static void test(int port) throws Exception {
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    86
        System.out.printf("%nClient using port number: %s%n", port);
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    87
        String spec = String.format("https://localhost:%s/test1/", port);
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    88
        URL url = new URL(spec);
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    89
        HttpsURLConnection urlcs = (HttpsURLConnection) url.openConnection();
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    90
        urlcs.setHostnameVerifier(new HostnameVerifier() {
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    91
            public boolean verify(String s, SSLSession s1) {
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    92
                return true;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    93
            }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    94
        });
39493
837860f3f432 8160940: Enable debug log in javax/net/ssl/HttpsURLConnection/Equals.java to track JDK-8160210
ssahoo
parents: 38327
diff changeset
    95
        urlcs.setSSLSocketFactory(ctx.getSocketFactory());
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    96
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    97
        InputStream is = urlcs.getInputStream();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    98
        while (is.read() != -1) {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    99
        }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   100
        is.close();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   101
        if (!urlcs.equals(urlcs)) {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   102
            throw new RuntimeException("Test failed");
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   103
        }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   104
    }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   105
}