test/jdk/java/net/URLConnection/Responses.java
author dfuchs
Tue, 07 May 2019 18:10:59 +0100
changeset 54746 61049e91eae5
parent 47216 71c04702a3d5
child 58009 0daf32316b47
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8223463: Replace wildcard address with loopback or local host in tests - part 2 Summary: Removes (or documents) some usages of the wildcard address in intermittently failing tests. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4635698
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that HttpURLConnection.getResponseCode returns -1 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          malformed status-lines in the http response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Responses {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * Test cases :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     *       "Response from server"     expected getResponse() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     *                                  getResponseMessage() results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static Object[][] getTests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return new Object[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            { "HTTP/1.1 200 OK",        "200",  "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            { "HTTP/1.1 404 ",          "404",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            { "HTTP/1.1 200",           "200",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            { "HTTP/1.1",               "-1",   null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            { "Invalid",                "-1",   null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            { null,                     "-1" ,  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        };
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
     * Simple http server used by test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * GET /<n> HTTP/1.x results in http response with the status line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * set to geTests()[<n>][0]  -- eg: GET /2 results in a response of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * "HTTP/1.1 404 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static class HttpServer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public HttpServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    63
                InetAddress loopback = InetAddress.getLoopbackAddress();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    64
                ss = new ServerSocket();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    65
                ss.bind(new InetSocketAddress(loopback, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                throw new Error("Unable to create ServerSocket: " + ioe);
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
        public int port() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    75
        public String authority() {
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    76
            InetAddress address = ss.getInetAddress();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    77
            String hostaddr = address.isAnyLocalAddress()
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    78
                ? "localhost" : address.getHostAddress();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    79
            if (hostaddr.indexOf(':') > -1) {
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    80
                hostaddr = "[" + hostaddr + "]";
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    81
            }
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    82
            return hostaddr + ":" + port();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    83
        }
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    84
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public void shutdown() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Object[][] tests = getTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    BufferedReader in = new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                              new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                                s.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    String req = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    int pos1 = req.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    int pos2 = req.indexOf(' ', pos1+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    int i = Integer.parseInt(req.substring(pos1+2, pos2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                        new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                          s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    out.print( tests[i][0] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    out.print("Content-Length: 0\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    s.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        /* start the http server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        HttpServer svr = new HttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        (new Thread(svr)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
   131
        String authority = svr.authority();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
   132
        System.out.println("Server listening on: " + authority);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * Iterate through each test case and check that getResponseCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * returns the expected result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Object tests[][] = getTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i=0; i<tests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println("******************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.out.println("Test with response: >" + tests[i][0] + "<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
   145
            URL url = new URL("http://" + authority + "/" + i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // test getResponseCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                int expectedCode = Integer.parseInt((String)tests[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                int actualCode = http.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (actualCode != expectedCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    System.out.println("getResponseCode returned: " + actualCode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        ", expected: " + expectedCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                // test getResponseMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                String expectedPhrase = (String)tests[i][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                String actualPhrase = http.getResponseMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (actualPhrase == null && expectedPhrase == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (!actualPhrase.equals(expectedPhrase)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    System.out.println("getResponseMessage returned: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        actualPhrase + ", expected: " + expectedPhrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                failures++;
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
        /* shutdown http server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new Exception(failures + " sub-test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}