test/jdk/java/net/URLConnection/RedirectLimit.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58009 0daf32316b47
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54841
43439afaab4a 8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 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
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    26
 * @bug 4458085 7095949
54841
43439afaab4a 8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents: 47216
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary  Redirects Limited to 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Simulate a server that redirects ( to a different URL) 9 times
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and see if the client correctly follows the trail
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.*;
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
    38
import java.util.concurrent.CountDownLatch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
54841
43439afaab4a 8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents: 47216
diff changeset
    40
import jdk.test.lib.net.URIBuilder;
43439afaab4a 8223638: Replace wildcard address with loopback or local host in tests - part 6
aefimov
parents: 47216
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class RedirLimitServer extends Thread {
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
    43
    static final int TIMEOUT = 20 * 1000;
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    44
    static final int NUM_REDIRECTS = 9;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    46
    static final String reply1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "Location: http://localhost:";
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    50
    static final String reply2 = ".html\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "<html>Hello</html>";
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    54
    static final String reply3 = "HTTP/1.1 200 Ok\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "World";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    61
    final ServerSocket ss;
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    62
    final int port;
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
    63
    final CountDownLatch readyToStart = new CountDownLatch(1);
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    64
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    65
    RedirLimitServer(ServerSocket ss) throws IOException {
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    66
        this.ss = ss;
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    67
        port = this.ss.getLocalPort();
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    68
        this.ss.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    69
    }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    70
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    71
    static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    72
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    73
    // Read until the end of a HTTP request
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    74
    void readOneRequest(InputStream is) throws IOException {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    75
        int requestEndCount = 0, r;
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
    76
        StringBuilder sb = new StringBuilder();
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    77
        while ((r = is.read()) != -1) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
    78
            sb.append((char)r);
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    79
            if (r == requestEnd[requestEndCount]) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    80
                requestEndCount++;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    81
                if (requestEndCount == 4) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    82
                    break;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    83
                }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    84
            } else {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    85
                requestEndCount = 0;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    86
            }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    87
        }
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
    88
        System.out.println("Server got request: " + sb.toString());
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    89
    }
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    90
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    91
    public void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
    93
            readyToStart.countDown();
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    94
            for (int i=0; i<NUM_REDIRECTS; i++) {
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    95
                try (Socket s = ss.accept()) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
    96
                    System.out.println("Server accepted socket: " + s);
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
    97
                    s.setSoTimeout(TIMEOUT);
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
    98
                    readOneRequest(s.getInputStream());
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
    99
                    System.out.println("Redirecting to: /redirect" + i);
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   100
                    String reply = reply1 + port + "/redirect" + i + reply2;
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   101
                    s.getOutputStream().write(reply.getBytes());
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   102
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   104
            try (Socket s = ss.accept()) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
   105
                System.out.println("Server accepted socket: " + s);
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   106
                s.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   107
                readOneRequest(s.getInputStream());
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 57686
diff changeset
   108
                System.out.println("Replying...");
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 8547
diff changeset
   109
                s.getOutputStream().write(reply3.getBytes());
8547
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   110
            }
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   111
        } catch (Exception e) {
5a1f87621165 7020136: java/net/URLConnection/RedirectLimit.java fails intermittently
chegar
parents: 7668
diff changeset
   112
            e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
public class RedirectLimit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static void main(String[] args) throws Exception {
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   119
        try (ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress())) {
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   120
            int port = ss.getLocalPort();
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   121
            RedirLimitServer server = new RedirLimitServer(ss);
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   122
            server.start();
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   123
            server.readyToStart.await();
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   124
            URL url = URIBuilder.newBuilder()
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   125
                    .scheme("http")
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   126
                    .loopback()
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   127
                    .port(port)
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   128
                    .toURL();
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   129
            URLConnection conURL = url.openConnection(Proxy.NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   131
            conURL.setDoInput(true);
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   132
            conURL.setAllowUserInteraction(false);
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   133
            conURL.setUseCaches(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
57686
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   135
            try (InputStream in = conURL.getInputStream()) {
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   136
                if ((in.read() != (int) 'W') || (in.read() != (int) 'o')) {
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   137
                    throw new RuntimeException("Unexpected string read");
70f5cbb711a9 8225430: Replace wildcard address with loopback or local host in tests - part 14
aefimov
parents: 54841
diff changeset
   138
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}