test/jdk/java/net/URLConnection/B5052093.java
author chegar
Fri, 27 Sep 2019 09:55:35 +0100
changeset 58365 73950479184b
parent 54681 edd709e64ea1
permissions -rw-r--r--
8231504: Update networking tests to avoid implicit dependency on the system proxies Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2007, 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
5165
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.
2
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: 5165
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5165
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5165
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 5052093
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    27
 * @modules java.base/sun.net.www java.base/sun.net.www.protocol.file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
    29
 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main B5052093
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary URLConnection doesn't support large files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.net.www.protocol.file.FileURLConnection;
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 54681
diff changeset
    36
import static java.net.Proxy.NO_PROXY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class B5052093 implements HttpCallback {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
    39
    private static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static long testSize = ((long) (Integer.MAX_VALUE)) + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static class LargeFile extends File {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        public LargeFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            super("/dev/zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            return testSize;
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
    public static class LargeFileURLConnection extends FileURLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public LargeFileURLConnection(LargeFile f) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                super(new URL("file:///dev/zero"), f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void request(HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            req.setResponseHeader("content-length", Long.toString(testSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            req.sendResponse(200, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 47216
diff changeset
    68
        InetAddress loopback = InetAddress.getLoopbackAddress();
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 47216
diff changeset
    69
        server = new TestHttpServer(new B5052093(), 1, 10, loopback, 0);
5165
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    70
        try {
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 47216
diff changeset
    71
            URL url = new URL("http://" + server.getAuthority() + "/foo");
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 54681
diff changeset
    72
            URLConnection conn = url.openConnection(NO_PROXY);
5165
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    73
            int i = conn.getContentLength();
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    74
            long l = conn.getContentLengthLong();
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    75
            if (i != -1 || l != testSize) {
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    76
                System.out.println("conn.getContentLength = " + i);
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    77
                System.out.println("conn.getContentLengthLong = " + l);
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    78
                System.out.println("testSize = " + testSize);
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    79
                throw new RuntimeException("Wrong content-length from http");
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    80
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
5165
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    82
            URLConnection fu = new LargeFileURLConnection(new LargeFile());
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    83
            i = fu.getContentLength();
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    84
            l = fu.getContentLengthLong();
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    85
            if (i != -1 || l != testSize) {
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    86
                System.out.println("fu.getContentLength = " + i);
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    87
                System.out.println("fu.getContentLengthLong = " + l);
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    88
                System.out.println("testSize = " + testSize);
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    89
                throw new RuntimeException("Wrong content-length from file");
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    90
            }
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    91
        } finally {
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    92
            server.terminate();
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    93
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}