jdk/test/java/net/Authenticator/B4678055.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 5506 202f599c92aa
child 13788 3f38e525f30a
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../../sun/net/www/httptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main B4678055
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary Basic Authentication fails with multiple realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class B4678055 implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static String authstring;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    void errorReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        req.addResponseHeader ("WWW-Authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        req.sendResponse (401, "Unauthorized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    void okReply (HttpTransaction req) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public void request (HttpTransaction req) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            authstring = req.getRequestHeader ("Authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.out.println (authstring);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                /* client stores a username/pw for wallyworld
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                /* emulates a server that has configured a second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                 * realm, but by misconfiguration uses the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                 * realm string as the previous one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                 * An alternative (more likely) scenario that shows this behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                 * the case where the password in the original realm has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                /* The client replies with the username/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                 * from the first realm, which is wrong (unexpectedly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                errorReply (req, "Basic realm=\"wallyworld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                /* The client re-prompts for a password and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                 * we now reply with an OK. The client with the bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                 * will throw NPE at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                /* Repeat the OK, to make sure the same new auth string is sent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static boolean checkFinalAuth () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return authstring.equals ("Basic dXNlcjpwYXNzMg==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void client (String u) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        URL url = new URL (u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println ("client opening connection to: " + u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            server = new HttpServer (new B4678055(), 1, 10, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println ("Server: listening on port: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            client ("http://localhost:"+server.getLocalPort()+"/d2/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            client ("http://localhost:"+server.getLocalPort()+"/d2/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (f != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            except ("Authenticator was called "+f+" times. Should be 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* this checks the authorization string corresponding to second password "pass2"*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (!checkFinalAuth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            except ("Wrong authorization string received from client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static void except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                pw = new PasswordAuthentication ("user", "pass1".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                pw = new PasswordAuthentication ("user", "pass2".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public int getCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return (count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}