jdk/test/java/net/Authenticator/B4769350.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4916 de44744708a2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 4769350
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 AbstractCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main B4769350 server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main B4769350 proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary proxy authentication username and password caching only works in serial case
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class B4769350 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            //System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static class Client extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String authority, path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        boolean allowerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Client (String authority, String path, boolean allowerror) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            this.authority = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            this.allowerror = allowerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                URI u = new URI ("http", authority, path, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                URL url = u.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } catch (URISyntaxException  e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (!allowerror) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
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
    static class CallBack extends AbstractCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        void errorReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            req.addResponseHeader ("WWW-Authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            req.sendResponse (401, "Unauthorized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        void proxyReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            req.addResponseHeader ("Proxy-Authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            req.sendResponse (407, "Proxy Authentication Required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        void okReply (HttpTransaction req) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public void request (HttpTransaction req, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                URI uri = req.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                String path = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (path.endsWith ("/t1a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    doT1a (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                } else if (path.endsWith ("/t1b")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    doT1b (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                } else if (path.endsWith ("/t1c")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    doT1c (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                } else if (path.endsWith ("/t1d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    doT1d (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } else if (path.endsWith ("/t2a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    doT2a (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } else if (path.endsWith ("/t2b")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    doT2b (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } else if (path.endsWith ("/t3a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    doT3a (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                } else if (path.endsWith ("/t3b")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    doT3bc (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else if (path.endsWith ("/t3c")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    doT3bc (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                   System.out.println ("unexpected request URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        /* T1 tests the client by sending 4 requests to 2 different realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * in parallel. The client should recognise two pairs of dependent requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * and execute the first of each pair in parallel. When they both succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * the second requests should be executed without calling the authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * The test succeeds if the authenticator was only called twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        void doT1a (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                errorReply (req, "Basic realm=\"realm1\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                HttpServer.rendezvous ("one", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                HttpServer.waitForCondition ("cond2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        void doT1b (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                errorReply (req, "Basic realm=\"realm2\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                HttpServer.rendezvous ("one", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                HttpServer.setCondition ("cond1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                HttpServer.waitForCondition ("cond2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        void doT1c (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                errorReply (req, "Basic realm=\"realm1\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                HttpServer.rendezvous ("two", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        void doT1d (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                errorReply (req, "Basic realm=\"realm2\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                HttpServer.rendezvous ("two", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                HttpServer.setCondition ("cond2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        /* T2 tests to check that if initial authentication fails, the second will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * succeed, and the authenticator is called twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        void doT2a (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            /* This will be called several times */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                HttpServer.setCondition ("T2cond1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            errorReply (req, "Basic realm=\"realm3\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        void doT2b (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                errorReply (req, "Basic realm=\"realm3\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                System.out.println ("Unexpected request");
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
        /* T3 tests proxy and server authentication. three threads request same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * resource at same time. Authenticator should be called once for server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * and once for proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        void doT3a (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                proxyReply (req, "Basic realm=\"proxy\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                HttpServer.setCondition ("T3cond1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                errorReply (req, "Basic realm=\"realm4\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        void doT3bc (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                proxyReply (req, "Basic realm=\"proxy\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    static int redirects = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static void doServerTests (String authority) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        System.out.println ("Doing Server tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        System.out.println ("T1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        c1 = new Client (authority, "/test/realm1/t1a", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        c2 = new Client (authority, "/test/realm2/t1b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        c3 = new Client (authority, "/test/realm1/t1c", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        c4 = new Client (authority, "/test/realm2/t1d", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        c1.start(); c2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        HttpServer.waitForCondition ("cond1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        c3.start(); c4.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        c1.join(); c2.join(); c3.join(); c4.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (f != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            except ("Authenticator was called "+f+" times. Should be 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            except ("error occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        auth.resetCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        System.out.println ("T2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        c5 = new Client (authority, "/test/realm3/t2a", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        c6 = new Client (authority, "/test/realm3/t2b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        c5.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        HttpServer.waitForCondition ("T2cond1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        c6.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        c5.join(); c6.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (f != redirects+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            except ("Authenticator was called "+f+" times. Should be: " + redirects+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            except ("error occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    static void doProxyTests (String authority) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        System.out.println ("Doing Proxy tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        c7 = new Client (authority, "/test/realm4/t3a", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        c8 = new Client (authority, "/test/realm4/t3b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        c9 = new Client (authority, "/test/realm4/t3c", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        c7.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        HttpServer.waitForCondition ("T3cond1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        c8.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        c9.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        c7.join(); c8.join(); c9.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (f != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            except ("Authenticator was called "+f+" times. Should be: " + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            except ("error occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        System.setProperty ("http.maxRedirects", Integer.toString (redirects));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        System.setProperty ("http.auth.serializeRequests", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        boolean proxy = args[0].equals ("proxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            server = new HttpServer (new CallBack(), 10, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            System.out.println ("Server: listening on port: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (proxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                System.setProperty ("http.proxyHost", "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                System.setProperty ("http.proxyPort",Integer.toString(server.getLocalPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                doProxyTests ("www.foo.com");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                doServerTests ("localhost:"+server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    static void pause (int millis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            Thread.sleep (millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public static void except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            //System.out.println ("Authenticator called: " + getRequestingPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            //try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                //Thread.sleep (1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            //} catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            pw = new PasswordAuthentication ("user", "pass1".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        public void resetCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        public int getCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return (count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}