jdk/test/java/net/Authenticator/B4769350.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 13788 3f38e525f30a
child 21320 0a56bf0c2390
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13788
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4916
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4916
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4916
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 4769350
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
    28
 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction AbstractCallback
4916
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 2
diff changeset
    29
 * @run main/othervm B4769350 server
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 2
diff changeset
    30
 * @run main/othervm B4769350 proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary proxy authentication username and password caching only works in serial case
4916
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 2
diff changeset
    32
 * Run in othervm since the test sets system properties that are read by the
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 2
diff changeset
    33
 * networking stack and cached when the HTTP handler is invoked, and previous
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 2
diff changeset
    34
 * tests may already have invoked the HTTP handler.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class B4769350 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            //System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static class Client extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String authority, path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        boolean allowerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Client (String authority, String path, boolean allowerror) {
5979
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
    57
            super("Thread-" + path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            this.authority = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.allowerror = allowerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                URI u = new URI ("http", authority, path, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                URL url = u.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                InputStream is = urlc.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } catch (URISyntaxException  e) {
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
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (!allowerror) {
5979
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
    76
                    System.out.println (Thread.currentThread().getName() + " " + e);
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
    77
                    e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
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
    static class CallBack extends AbstractCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        void errorReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            req.addResponseHeader ("Connection", "close");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            req.addResponseHeader ("WWW-Authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            req.sendResponse (401, "Unauthorized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            req.orderlyClose();
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 proxyReply (HttpTransaction req, String reply) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            req.addResponseHeader ("Proxy-Authenticate", reply);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            req.sendResponse (407, "Proxy Authentication Required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        void okReply (HttpTransaction req) throws IOException {
5979
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
    99
            req.addResponseHeader ("Connection", "close");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            req.setResponseEntityBody ("Hello .");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            req.sendResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public void request (HttpTransaction req, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                URI uri = req.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                String path = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (path.endsWith ("/t1a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    doT1a (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                } else if (path.endsWith ("/t1b")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    doT1b (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } else if (path.endsWith ("/t1c")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    doT1c (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                } else if (path.endsWith ("/t1d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    doT1d (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                } else if (path.endsWith ("/t2a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    doT2a (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else if (path.endsWith ("/t2b")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    doT2b (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                } else if (path.endsWith ("/t3a")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    doT3a (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                } else if (path.endsWith ("/t3b")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    doT3bc (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                } else if (path.endsWith ("/t3c")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    doT3bc (req, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                   System.out.println ("unexpected request URI");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /* T1 tests the client by sending 4 requests to 2 different realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * in parallel. The client should recognise two pairs of dependent requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * and execute the first of each pair in parallel. When they both succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * the second requests should be executed without calling the authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * The test succeeds if the authenticator was only called twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        void doT1a (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                errorReply (req, "Basic realm=\"realm1\"");
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   145
                TestHttpServer.rendezvous ("one", 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            case 1:
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   148
                TestHttpServer.waitForCondition ("cond2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        void doT1b (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                errorReply (req, "Basic realm=\"realm2\"");
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   161
                TestHttpServer.rendezvous ("one", 2);
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   162
                TestHttpServer.setCondition ("cond1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            case 1:
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   165
                TestHttpServer.waitForCondition ("cond2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        void doT1c (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                errorReply (req, "Basic realm=\"realm1\"");
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   177
                TestHttpServer.rendezvous ("two", 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        void doT1d (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                errorReply (req, "Basic realm=\"realm2\"");
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   191
                TestHttpServer.rendezvous ("two", 2);
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   192
                TestHttpServer.setCondition ("cond2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /* T2 tests to check that if initial authentication fails, the second will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * succeed, and the authenticator is called twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        void doT2a (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            /* This will be called several times */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (count == 1) {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   210
                TestHttpServer.setCondition ("T2cond1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            errorReply (req, "Basic realm=\"realm3\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        void doT2b (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                errorReply (req, "Basic realm=\"realm3\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        /* T3 tests proxy and server authentication. three threads request same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         * resource at same time. Authenticator should be called once for server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
         * and once for proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        void doT3a (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                proxyReply (req, "Basic realm=\"proxy\"");
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   236
                TestHttpServer.setCondition ("T3cond1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                errorReply (req, "Basic realm=\"realm4\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        void doT3bc (HttpTransaction req, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                proxyReply (req, "Basic realm=\"proxy\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                okReply (req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                System.out.println ("Unexpected request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   263
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    static MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    static int redirects = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    static void doServerTests (String authority) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        System.out.println ("Doing Server tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        System.out.println ("T1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        c1 = new Client (authority, "/test/realm1/t1a", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        c2 = new Client (authority, "/test/realm2/t1b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        c3 = new Client (authority, "/test/realm1/t1c", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        c4 = new Client (authority, "/test/realm2/t1d", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        c1.start(); c2.start();
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   279
        TestHttpServer.waitForCondition ("cond1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        c3.start(); c4.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        c1.join(); c2.join(); c3.join(); c4.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (f != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            except ("Authenticator was called "+f+" times. Should be 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            except ("error occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        auth.resetCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        System.out.println ("T2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        c5 = new Client (authority, "/test/realm3/t2a", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        c6 = new Client (authority, "/test/realm3/t2b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        c5.start ();
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   297
        TestHttpServer.waitForCondition ("T2cond1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        c6.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        c5.join(); c6.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (f != redirects+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            except ("Authenticator was called "+f+" times. Should be: " + redirects+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            except ("error occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    static void doProxyTests (String authority) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        System.out.println ("Doing Proxy tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        c7 = new Client (authority, "/test/realm4/t3a", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        c8 = new Client (authority, "/test/realm4/t3b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        c9 = new Client (authority, "/test/realm4/t3c", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        c7.start ();
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   316
        TestHttpServer.waitForCondition ("T3cond1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        c8.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        c9.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        c7.join(); c8.join(); c9.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (f != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            except ("Authenticator was called "+f+" times. Should be: " + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            except ("error occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        System.setProperty ("http.maxRedirects", Integer.toString (redirects));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        System.setProperty ("http.auth.serializeRequests", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Authenticator.setDefault (auth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        boolean proxy = args[0].equals ("proxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 7668
diff changeset
   336
            server = new TestHttpServer (new CallBack(), 10, 1, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            System.out.println ("Server: listening on port: " + server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (proxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                System.setProperty ("http.proxyHost", "localhost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                System.setProperty ("http.proxyPort",Integer.toString(server.getLocalPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                doProxyTests ("www.foo.com");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                doServerTests ("localhost:"+server.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static void pause (int millis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            Thread.sleep (millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public static void except (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            //System.out.println ("Authenticator called: " + getRequestingPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            //try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                //Thread.sleep (1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            //} catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            pw = new PasswordAuthentication ("user", "pass1".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public void resetCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public int getCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return (count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
}