jdk/test/java/net/Authenticator/B4769350.java
author mikael
Fri, 04 Dec 2015 15:08:49 -0800
changeset 35090 1f5b6aa795d0
parent 22976 90ec9fec7c7f
child 38883 d5de564f8089
permissions -rw-r--r--
8144748: Move assembler/macroAssembler inline function definitions to corresponding inline.hpp files Reviewed-by: kvn, coleenp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
     2
 * Copyright (c) 2002, 2013, 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
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    26
 * @bug 4769350 8017779
4916
de44744708a2 6693244: Java Web Start app fails on 6u10 beta w/ AssertionError in AuthenticationInfo.requestCompleted
chegar
parents: 2
diff changeset
    27
 * @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
    28
 * @run main/othervm B4769350 proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @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
    30
 * 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
    31
 * 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
    32
 * tests may already have invoked the HTTP handler.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    35
import com.sun.net.httpserver.HttpExchange;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    36
import com.sun.net.httpserver.HttpHandler;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    37
import com.sun.net.httpserver.HttpServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    40
import java.util.concurrent.BrokenBarrierException;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    41
import java.util.concurrent.CountDownLatch;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    42
import java.util.concurrent.CyclicBarrier;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    43
import java.util.concurrent.Executor;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    44
import java.util.concurrent.ExecutorService;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    45
import java.util.concurrent.Executors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class B4769350 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void read (InputStream is) throws IOException {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    53
        while (is.read() != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            //System.out.write (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    58
     static class Client extends Thread {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        String authority, path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        boolean allowerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Client (String authority, String path, boolean allowerror) {
5979
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
    63
            super("Thread-" + path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.authority = authority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.allowerror = allowerror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
    69
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                URI u = new URI ("http", authority, path, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                URL url = u.toURL();
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    74
                URLConnection urlc = url.openConnection();
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
    75
                try (InputStream is = urlc.getInputStream()) {
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
    76
                    read (is);
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
    77
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            } catch (URISyntaxException  e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                System.out.println (e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (!allowerror) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    83
                    System.out.println (Thread.currentThread().getName()
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    84
                            + " " + e);
5979
26b9b2b1b37b 6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents: 5506
diff changeset
    85
                    e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    92
    class Server implements AutoCloseable {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    93
        HttpServer server;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    94
        Executor executor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    96
        public String getAddress() {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
    97
            return server.getAddress().getHostName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   100
        public void startServer() {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   101
            InetSocketAddress addr = new InetSocketAddress(0);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            try {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   104
                server = HttpServer.create(addr, 0);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   105
            } catch (IOException ioe) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   106
                throw new RuntimeException("Server could not be created");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   108
            executor = Executors.newFixedThreadPool(10);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   109
            server.setExecutor(executor);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   110
            server.createContext("/test/realm1/t1a",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   111
                    new AuthenticationHandlerT1a() );
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   112
            server.createContext("/test/realm2/t1b",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   113
                    new AuthenticationHandlerT1b());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   114
            server.createContext("/test/realm1/t1c",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   115
                    new AuthenticationHandlerT1c());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   116
            server.createContext("/test/realm2/t1d",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   117
                    new AuthenticationHandlerT1d());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   118
            server.createContext("/test/realm3/t2a",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   119
                    new AuthenticationHandlerT2a());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   120
            server.createContext("/test/realm3/t2b",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   121
                    new AuthenticationHandlerT2b());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   122
            server.createContext("/test/realm4/t3a",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   123
                    new AuthenticationHandlerT3a());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   124
            server.createContext("/test/realm4/t3b",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   125
                    new AuthenticationHandlerT3bc());
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   126
            server.createContext("/test/realm4/t3c",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   127
                    new AuthenticationHandlerT3bc());
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   128
            t1Cond1 = new CyclicBarrier(3);
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   129
            server.start();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   130
        }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   131
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   132
        public int getPort() {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   133
            return server.getAddress().getPort();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   134
        }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   135
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   136
        @Override
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   137
        public void close() {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   138
            if (executor != null)
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   139
                ((ExecutorService)executor).shutdownNow();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   140
            if (server != null)
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   141
                server.stop(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /* T1 tests the client by sending 4 requests to 2 different realms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * in parallel. The client should recognise two pairs of dependent requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         * and execute the first of each pair in parallel. When they both succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * the second requests should be executed without calling the authenticator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         * The test succeeds if the authenticator was only called twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         */
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   150
        class AuthenticationHandlerT1a implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   151
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   152
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   153
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   154
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   155
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   156
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   157
                try {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   158
                    switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   159
                        case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   160
                            AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   161
                                    "Basic realm=\"realm1\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   162
                            break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   163
                        case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   164
                            t1Cond1.await();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   165
                            AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   166
                            break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   167
                        default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   168
                            System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   169
                    }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   170
                } catch (InterruptedException |
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   171
                                 BrokenBarrierException e)
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   172
                        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   173
                            throw new RuntimeException(e);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   174
                        }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   175
            }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   176
        }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   177
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   178
        class AuthenticationHandlerT1b implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   179
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   180
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   181
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   182
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   183
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   184
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   185
                try {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   186
                    switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   187
                        case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   188
                            AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   189
                                    "Basic realm=\"realm2\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   190
                            break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   191
                        case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   192
                            t1Cond1.await();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   193
                            AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   194
                            break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   195
                        default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   196
                            System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   197
                    }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   198
                } catch (InterruptedException | BrokenBarrierException e) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   199
                    throw new RuntimeException(e);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   200
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   204
        class AuthenticationHandlerT1c implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   205
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   206
            volatile int count = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   208
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   209
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   210
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   211
                switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   212
                    case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   213
                        AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   214
                                "Basic realm=\"realm1\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   215
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   216
                    case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   217
                        AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   218
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   219
                    default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   220
                        System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   221
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   225
        class AuthenticationHandlerT1d implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   226
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   227
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   228
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   229
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   230
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   231
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   232
                switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   233
                    case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   234
                        AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   235
                                "Basic realm=\"realm2\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   236
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   237
                    case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   238
                        AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   239
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   240
                    default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   241
                        System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   242
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        /* T2 tests to check that if initial authentication fails, the second will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * succeed, and the authenticator is called twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   250
        class AuthenticationHandlerT2a implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   251
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   252
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   253
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   254
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   255
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   256
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   257
                if (count == 1) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   258
                    t2condlatch.countDown();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   259
                }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   260
                AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   261
                        "Basic realm=\"realm3\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   262
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   266
         class AuthenticationHandlerT2b implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   267
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   268
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   269
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   270
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   271
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   272
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   273
                switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   274
                    case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   275
                        AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   276
                                "Basic realm=\"realm3\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   277
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   278
                    case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   279
                        AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   280
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   281
                    default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   282
                        System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   283
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /* T3 tests proxy and server authentication. three threads request same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * resource at same time. Authenticator should be called once for server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * and once for proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         */
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   291
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   292
        class AuthenticationHandlerT3a implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   293
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   294
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   295
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   296
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   297
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   298
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   299
                switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   300
                    case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   301
                        AuthenticationHandler.proxyReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   302
                                "Basic realm=\"proxy\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   303
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   304
                    case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   305
                        t3cond1.countDown();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   306
                        AuthenticationHandler.errorReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   307
                                "Basic realm=\"realm4\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   308
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   309
                    case 2:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   310
                        AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   311
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   312
                    default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   313
                        System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   314
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   318
        class AuthenticationHandlerT3bc implements HttpHandler
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   319
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   320
            volatile int count = -1;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   321
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   322
            @Override
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   323
            public void handle(HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   324
                count++;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   325
                switch(count) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   326
                    case 0:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   327
                        AuthenticationHandler.proxyReply(exchange,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   328
                                "Basic realm=\"proxy\"");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   329
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   330
                    case 1:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   331
                        AuthenticationHandler.okReply(exchange);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   332
                        break;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   333
                    default:
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   334
                        System.out.println ("Unexpected request");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   335
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   338
    }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   339
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   340
    static class AuthenticationHandler {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   341
        static void errorReply(HttpExchange exchange, String reply)
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   342
                throws IOException
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   343
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   344
            exchange.getResponseHeaders().add("Connection", "close");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   345
            exchange.getResponseHeaders().add("WWW-Authenticate", reply);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   346
            exchange.sendResponseHeaders(401, 0);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   347
            exchange.close();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   348
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   350
        static void proxyReply (HttpExchange exchange, String reply)
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   351
                throws IOException
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   352
        {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   353
            exchange.getResponseHeaders().add("Proxy-Authenticate", reply);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   354
            exchange.sendResponseHeaders(407, 0);
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   355
        }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   356
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   357
        static void okReply (HttpExchange exchange) throws IOException {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   358
            exchange.getResponseHeaders().add("Connection", "close");
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   359
            String response = "Hello .";
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   360
            exchange.sendResponseHeaders(200, response.getBytes().length);
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   361
            try (OutputStream os = exchange.getResponseBody()) {
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   362
                os.write(response.getBytes());
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   363
            }
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   364
            exchange.close();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   365
        }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   366
    }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   367
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   368
    static Server server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    static MyAuthenticator auth = new MyAuthenticator ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    static int redirects = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    static Client c1,c2,c3,c4,c5,c6,c7,c8,c9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   375
    static CountDownLatch t2condlatch;
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   376
    static CountDownLatch t3cond1;
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   377
    static CyclicBarrier t1Cond1;
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   378
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   379
    static void doServerTests (String authority, Server server) throws Exception
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   380
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        System.out.println ("Doing Server tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        System.out.println ("T1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        c1 = new Client (authority, "/test/realm1/t1a", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        c2 = new Client (authority, "/test/realm2/t1b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        c3 = new Client (authority, "/test/realm1/t1c", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        c4 = new Client (authority, "/test/realm2/t1d", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        c1.start(); c2.start();
22976
90ec9fec7c7f 8031661: java/net/Authenticator/B4769350.java failed intermittently
michaelm
parents: 21320
diff changeset
   388
        t1Cond1.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        c3.start(); c4.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        c1.join(); c2.join(); c3.join(); c4.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (f != 2) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   394
            except ("Authenticator was called "+f+" times. Should be 2",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   395
                    server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (error) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   398
            except ("error occurred", server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        auth.resetCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        System.out.println ("T2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        c5 = new Client (authority, "/test/realm3/t2a", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        c6 = new Client (authority, "/test/realm3/t2b", false);
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   406
        t2condlatch = new CountDownLatch(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        c5.start ();
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   408
        t2condlatch.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        c6.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        c5.join(); c6.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (f != redirects+1) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   414
            except ("Authenticator was called "+f+" times. Should be: "
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   415
                    + redirects+1, server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (error) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   418
            except ("error occurred", server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   422
    static void doProxyTests (String authority, Server server) throws Exception
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   423
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        System.out.println ("Doing Proxy tests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        c7 = new Client (authority, "/test/realm4/t3a", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        c8 = new Client (authority, "/test/realm4/t3b", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        c9 = new Client (authority, "/test/realm4/t3c", false);
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   428
        t3cond1 = new CountDownLatch(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        c7.start ();
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   430
        t3cond1.await();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        c8.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        c9.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        c7.join(); c8.join(); c9.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (f != 2) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   437
            except ("Authenticator was called "+f+" times. Should be: " + 2,
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   438
                    server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (error) {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   441
            except ("error occurred", server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public static void main (String[] args) throws Exception {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   446
        new B4769350().runTest(args[0].equals ("proxy"));
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   447
    }
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   448
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   449
    public void runTest(boolean proxy) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        System.setProperty ("http.maxRedirects", Integer.toString (redirects));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        System.setProperty ("http.auth.serializeRequests", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Authenticator.setDefault (auth);
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   453
        try (Server server = new Server()) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   454
            server.startServer();
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   455
            System.out.println ("Server: listening on port: "
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   456
                    + server.getPort());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (proxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                System.setProperty ("http.proxyHost", "localhost");
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   459
                System.setProperty ("http.proxyPort",
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   460
                        Integer.toString(server.getPort()));
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   461
                doProxyTests ("www.foo.com", server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } else {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   463
                doServerTests ("localhost:"+server.getPort(), server);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   465
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   469
    public static void except (String s, Server server) {
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   470
        server.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        throw new RuntimeException (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    static class MyAuthenticator extends Authenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        MyAuthenticator () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            super ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   479
        volatile int count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   481
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        public PasswordAuthentication getPasswordAuthentication () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            PasswordAuthentication pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            pw = new PasswordAuthentication ("user", "pass1".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        public void resetCount () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        public int getCount () {
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   494
            return count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}
21320
0a56bf0c2390 8017779: java/net/Authenticator/B4769350.java fails
chegar
parents: 14342
diff changeset
   498