test/jdk/com/sun/net/httpserver/bugs/B6401598.java
author dfuchs
Mon, 10 Jun 2019 11:17:57 +0100
changeset 55309 8081b181bba8
parent 54314 46cf212cdcca
permissions -rw-r--r--
8224865: Replace wildcard address with loopback or local host in tests - part 13 Summary: Fixes intermittent failures observed in some network tests Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55309
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
     2
 * Copyright (c) 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    26
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 6401598
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary  new HttpServer cannot serve binary stream data
55309
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
    29
 * @run main B6401598
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv6Addresses=true B6401598
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.MalformedURLException;
55309
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
    36
import java.net.Proxy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URL;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    38
import java.net.InetAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    42
import jdk.test.lib.net.URIBuilder;
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import com.sun.net.httpserver.HttpHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import com.sun.net.httpserver.HttpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class B6401598 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        static class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                public MyHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                public void handle(HttpExchange arg0) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                InputStream is = arg0.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                OutputStream os = arg0.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                short input = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                while (dis.read() != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                DataOutputStream dos = new DataOutputStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                arg0.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                dos.writeShort(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        }
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
        static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        static ExecutorService exec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                try {
55309
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
    90
                        InetAddress loopback = InetAddress.getLoopbackAddress();
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
    91
                        server = HttpServer.create(new InetSocketAddress(loopback, 0), 400);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        server.createContext("/server/", new MyHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        exec = Executors.newFixedThreadPool(3);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    94
                        server.setExecutor(exec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        short counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        for (counter = 0; counter < 1000; counter++) {
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   101
                                URL url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   102
                                    .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   103
                                    .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   104
                                    .port(port)
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   105
                                    .path("/server/")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   106
                                    .toURLUnchecked();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   107
                                System.out.println("URL: " + url);
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   108
                                HttpURLConnection connection = getHttpURLConnection(url, 10000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                OutputStream os = connection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                DataOutputStream dos = new DataOutputStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                dos.writeShort(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                InputStream is = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                short ret = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        System.out.println ("Stopping");
55309
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
   130
                } catch (Exception e) {
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
   131
                    throw new AssertionError("Unexpected exception: " + e, e);
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
   132
                } finally {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        server.stop (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        exec.shutdown();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
55309
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
   142
                HttpURLConnection httpURLConnection =
8081b181bba8 8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents: 54314
diff changeset
   143
                    (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                httpURLConnection.setConnectTimeout(40000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                httpURLConnection.setReadTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                httpURLConnection.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                httpURLConnection.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                httpURLConnection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                httpURLConnection.setAllowUserInteraction(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                httpURLConnection.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                return httpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}