test/jdk/java/net/Authenticator/B4678055.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
/*
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
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 4678055
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    27
 * @modules java.base/sun.net.www
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    28
 * @library ../../../sun/net/www/httptest/ /test/lib
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    29
 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    30
 * @run main/othervm B4678055
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 B4678055
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @summary Basic Authentication fails with multiple realms
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.*;
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
    37
import jdk.test.lib.net.URIBuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class B4678055 implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
    41
    static volatile int count = 0;
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
    42
    static volatile String authstring;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    void errorReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        req.addResponseHeader ("WWW-Authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        req.sendResponse (401, "Unauthorized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    void okReply (HttpTransaction req) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
    59
            System.out.println("Server handling case: "+ count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            authstring = req.getRequestHeader ("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println (authstring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                /* client stores a username/pw for wallyworld
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                /* emulates a server that has configured a second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                 * realm, but by misconfiguration uses the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                 * realm string as the previous one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                 * An alternative (more likely) scenario that shows this behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                 * the case where the password in the original realm has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                /* The client replies with the username/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                 * from the first realm, which is wrong (unexpectedly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                /* The client re-prompts for a password and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                 * we now reply with an OK. The client with the bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                 * will throw NPE at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                /* Repeat the OK, to make sure the same new auth string is sent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (IOException e) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
    99
            System.err.println("Unexpected exception for case " + count + ": " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static boolean checkFinalAuth () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return authstring.equals ("Basic dXNlcjpwYXNzMg==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static void client (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
   127
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Authenticator.setDefault (auth);
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   132
        ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   134
            InetAddress loopback = InetAddress.getLoopbackAddress();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   135
            server = new TestHttpServer(new B4678055(), 1, 10, loopback, 0);
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   136
            String serverURL = URIBuilder.newBuilder()
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   137
                .scheme("http")
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   138
                .loopback()
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   139
                .port(server.getLocalPort())
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   140
                .path("/")
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   141
                .build()
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   142
                .toString();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   143
            System.out.println("Server: listening at: " + serverURL);
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   144
            client(serverURL + "d1/foo.html");
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   145
            client(serverURL + "d2/foo.html");
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 47216
diff changeset
   146
            client(serverURL + "d2/foo.html");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (Exception e) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
   148
            System.out.println("Client got exception: " + e);
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
   149
            System.out.println("Terminating server");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (f != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            except ("Authenticator was called "+f+" times. Should be 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /* this checks the authorization string corresponding to second password "pass2"*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!checkFinalAuth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            except ("Wrong authorization string received from client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
   163
        System.out.println("Terminating server");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static void except (String s) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
   168
        System.out.println("Check failed: " + s);
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
   169
        System.out.println("Terminating server");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 55399
diff changeset
   179
        volatile int count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                pw = new PasswordAuthentication ("user", "pass1".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                pw = new PasswordAuthentication ("user", "pass2".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        public int getCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return (count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}