test/jdk/java/net/Authenticator/B4962064.java
author dfuchs
Mon, 17 Jun 2019 20:03:34 +0100
changeset 55399 46049b8a5658
parent 47216 71c04702a3d5
permissions -rw-r--r--
8225578: Replace wildcard address with loopback or local host in tests - part 16 Summary: Fixes java/net/Authenticator and java/net/CookieHandler to stop depending on the wildcard address, wherever possible. Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2004, 2012, 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 4962064
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    27
 * @modules java.base/sun.net.www
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    29
 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main/othervm B4962064
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    31
 * @run main/othervm -Djava.net.preferIPv6Addresses=true B4962064
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary Extend Authenticator to provide access to request URI and server/proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class B4962064 implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
              case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                req.sendResponse (401, "Unauthorized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
              case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
              case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
              case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                req.addResponseHeader ("Proxy-Authenticate", "Basic realm=\"foo\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                req.sendResponse (407, "Proxy Authentication Required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println ("finished reading");
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
    static void client (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    90
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static URL urlsave;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    95
            InetAddress address = InetAddress.getLoopbackAddress();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    96
            InetAddress resolved = InetAddress.getByName(address.getHostName());
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    97
            System.out.println("Lookup: " + address + " -> \""
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    98
                               + address.getHostName() + "\" -> "
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    99
                               + resolved);
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   100
            server = new TestHttpServer (new B4962064(), 1, 10, address, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int port = server.getLocalPort();
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   102
            String proxyHost = address.equals(resolved)
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   103
                ? address.getHostName()
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   104
                : address.getHostAddress();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   105
            System.setProperty ("http.proxyHost", proxyHost);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.setProperty ("http.proxyPort", Integer.toString (port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.out.println ("Server started: listening on port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            String s = new String ("http://foo.com/d1/d2/d3/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            urlsave = new URL (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            client (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            s = new String ("http://bar.com/dr/d3/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            urlsave = new URL (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            client (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        server.terminate();
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 except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            URL url = getRequestingURL ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (!url.equals (urlsave)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                except ("urls not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Authenticator.RequestorType expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                expected = Authenticator.RequestorType.SERVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                expected = Authenticator.RequestorType.PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (getRequestorType() != expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                except ("wrong authtype");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}