jdk/test/javax/net/ssl/HttpsURLConnection/Equals.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 26221 47fd8b933a5c
child 38327 77143af4d719
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26221
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     1
/*
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    26
 * @library /lib/testlibrary
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    27
 * @build jdk.testlibrary.SimpleSSLContext
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    28
 * @run main Equals
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    29
 * @bug 8055299
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    30
 */
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
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    34
import java.net.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    35
import java.util.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    36
import java.io.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    37
import javax.net.ssl.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    38
import java.util.concurrent.*;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    39
import jdk.testlibrary.SimpleSSLContext;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    40
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    41
public class Equals {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    42
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    43
    static SSLContext ctx;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    44
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    45
    public static void main (String[] args) throws Exception {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    46
        HttpsServer s2 = null;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    47
        ExecutorService executor=null;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    48
        try {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    49
            InetSocketAddress addr = new InetSocketAddress (0);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    50
            s2 = HttpsServer.create (addr, 0);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    51
            HttpHandler h = new Handler ();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    52
            HttpContext c2 = s2.createContext ("/test1", h);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    53
            executor = Executors.newCachedThreadPool();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    54
            s2.setExecutor (executor);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    55
            ctx = new SimpleSSLContext().get();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    56
            s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    57
            s2.start();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    58
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    59
            int httpsport = s2.getAddress().getPort();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    60
            test(httpsport);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    61
            System.out.println ("OK");
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    62
        } finally {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    63
            if (s2 != null)
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    64
                s2.stop(2);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    65
            if (executor != null)
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    66
                executor.shutdown ();
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 {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    71
        int invocation = 1;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    72
        public void handle (HttpExchange t)
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    73
            throws IOException
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    74
        {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    75
            InputStream is = t.getRequestBody();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    76
            while (is.read () != -1) {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    77
            }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    78
            is.close();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    79
            t.sendResponseHeaders (200, 0);
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    80
            t.close();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    81
        }
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
    static void test (int port) throws Exception {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    85
        URL url = new URL ("https://localhost:"+port+"/test1/");
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    86
        HttpsURLConnection urlcs = (HttpsURLConnection) url.openConnection();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    87
        urlcs.setHostnameVerifier (new HostnameVerifier () {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    88
            public boolean verify (String s, SSLSession s1) {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    89
                return true;
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    90
            }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    91
        });
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    92
        urlcs.setSSLSocketFactory (ctx.getSocketFactory());
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    93
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    94
        InputStream is = urlcs.getInputStream();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    95
        while (is.read() != -1) {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    96
        }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    97
        is.close();
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    98
        if (!urlcs.equals(urlcs)) {
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
    99
            throw new RuntimeException("Test failed");
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   100
        }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   101
    }
47fd8b933a5c 8055299: HttpsURLConnection.equals() broken
michaelm
parents:
diff changeset
   102
}