jdk/test/java/net/Authenticator/BasicTest4.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @bug 4623722
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary  performance hit for Basic Authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class BasicTest4 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static class BasicServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        static final String realm = "wallyworld";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            "WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String reply2 = "HTTP/1.1 200 OK\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            "Content-Type: text/html; charset=iso-8859-1\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            "Content-Length: 10\r\n\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        BasicServer (ServerSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            server = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        static boolean checkFor (InputStream in, char[] seq) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println ("checkfor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                int i=0, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    int c = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    if (c == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    if (c == seq[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        if (i == seq.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        void readAll (Socket s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            byte[] buf = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            s.setSoTimeout(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                while (is.read(buf) > 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } catch (SocketTimeoutException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                System.out.println ("Server 1: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                os.write (reply1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                System.out.println ("Server 2: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                os.write ((reply2+"HelloWorld").getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                /* Second request now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                System.out.println ("Server 3: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                os.write (reply1.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.out.println ("Server 4: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                os.write ((reply2+"HelloAgain").getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                /* Third request now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                /* This should include pre-emptive authorization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                System.out.println ("Server 5: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                s.setSoTimeout (1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                success = checkFor (is, "Authorization".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                System.out.println ("checkfor returned " + success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                os.write (reply2.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                if (success)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                System.out.println ("Server 6: accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                s = server.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                System.out.println ("accepted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                os = s.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                readAll (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                os.write ((reply2+"HelloAgain").getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                s.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            finished ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public synchronized void finished () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        public PasswordAuthentication getPasswordAuthentication ()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            System.out.println ("Auth called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        System.out.println ("reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        System.out.println ("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        System.out.println ("finished reading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        ServerSocket ss = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int port = ss.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        BasicServer server = new BasicServer (ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        synchronized (server) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            System.out.println ("client 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            URL url = new URL ("http://localhost:"+port+"/d1/d3/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            System.out.println ("client 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            url = new URL ("http://localhost:"+port+"/d1/d2/bar.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println ("client 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            url = new URL ("http://localhost:"+port+"/d1/d4/foobar.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            server.wait ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (!server.success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                throw new RuntimeException ("3rd request did not use pre-emptive authorization");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}