test/jdk/java/net/Authenticator/B4722333.java
author dfuchs
Fri, 24 May 2019 15:34:14 +0100
changeset 55031 37a077319427
parent 54681 edd709e64ea1
permissions -rw-r--r--
8224603: Replace wildcard address with loopback or local host in tests - part 11 Summary: Fixes a batch of tests that were observed failing intermittently. Reviewed-by: chegar, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4722333
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    27
 * @modules java.base/sun.net.www
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ../../../sun/net/www/httptest/
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
    29
 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    30
 * @run main/othervm B4722333
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @summary JRE Proxy Authentication Not Working with ISA2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class B4722333 implements HttpCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    41
    static String[][] expected = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
       /* scheme  realm/prompt */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        {"basic", "foo"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        {"basic", "foobar"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        {"digest", "biz"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        {"digest", "bizbar"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        {"digest", "foobiz"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    50
    public void request(HttpTransaction req) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    52
            if (count % 2 == 1) {
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    53
                req.setResponseEntityBody("Hello .");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    54
                req.sendResponse(200, "Ok");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                switch (count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                  case 0:
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    59
                    req.addResponseHeader("Connection", "close");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    60
                    req.addResponseHeader("WWW-Authenticate", "Basic realm=\"foo\"");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    61
                    req.addResponseHeader("WWW-Authenticate", "Foo realm=\"bar\"");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    62
                    req.sendResponse(401, "Unauthorized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                  case 2:
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    66
                    req.addResponseHeader("Connection", "close");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    67
                    req.addResponseHeader("WWW-Authenticate", "Basic realm=\"foobar\" Foo realm=\"bar\"");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    68
                    req.sendResponse(401, "Unauthorized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                  case 4:
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    71
                    req.addResponseHeader("Connection", "close");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    72
                    req.addResponseHeader("WWW-Authenticate", "Digest realm=biz domain=/foo nonce=thisisanonce ");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    73
                    req.addResponseHeader("WWW-Authenticate", "Basic realm=bizbar");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    74
                    req.sendResponse(401, "Unauthorized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                  case 6:
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    78
                    req.addResponseHeader("Connection", "close");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    79
                    req.addResponseHeader("WWW-Authenticate", "Digest realm=\"bizbar\" domain=/biz nonce=\"hereisanonce\" Basic realm=\"foobar\" Foo realm=\"bar\"");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    80
                    req.sendResponse(401, "Unauthorized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                  case 8:
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    84
                    req.addResponseHeader("Connection", "close");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    85
                    req.addResponseHeader("WWW-Authenticate", "Foo p1=1 p2=2 p3=3 p4=4 p5=5 p6=6 p7=7 p8=8 p9=10 Digest realm=foobiz domain=/foobiz nonce=newnonce");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    86
                    req.addResponseHeader("WWW-Authenticate", "Basic realm=bizbar");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    87
                    req.sendResponse(401, "Unauthorized");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    req.orderlyClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
    98
    static void read(InputStream is) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int c;
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   100
        System.out.println("reading");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        while ((c=is.read()) != -1) {
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   102
            System.out.write(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   104
        System.out.println("");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   105
        System.out.println("finished reading");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   109
    static void client(String u) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        URL url = new URL (u);
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   111
        System.out.println("client opening connection to: " + u);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        InputStream is = urlc.getInputStream ();
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   114
        read(is);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
13788
3f38e525f30a 6354758: rename old test HttpServer classes
chegar
parents: 5506
diff changeset
   118
    static TestHttpServer server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   120
    public static void main(String[] args) throws Exception {
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   121
        MyAuthenticator auth = new MyAuthenticator();
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   122
        Authenticator.setDefault(auth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 47216
diff changeset
   124
            InetAddress loopback = InetAddress.getLoopbackAddress();
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   125
            server = new TestHttpServer(new B4722333(), 1, 10, loopback, 0);
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   126
            System.out.println("Server started: listening on port: " + server.getLocalPort());
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   127
            client("http://" + server.getAuthority() + "/d1/d2/d3/foo.html");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   128
            client("http://" + server.getAuthority() + "/ASD/d3/x.html");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   129
            client("http://" + server.getAuthority() + "/biz/d3/x.html");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   130
            client("http://" + server.getAuthority() + "/bar/d3/x.html");
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   131
            client("http://" + server.getAuthority() + "/fuzz/d3/x.html");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (server != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int f = auth.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (f != expected.length) {
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   140
            except("Authenticator was called "+f+" times. Should be " + expected.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   145
    public static void except(String s) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        server.terminate();
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   147
        throw new RuntimeException(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static class MyAuthenticator extends Authenticator {
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   151
        MyAuthenticator() {
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   152
            super();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   157
        public PasswordAuthentication getPasswordAuthentication() {
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   158
            System.out.println("Auth called");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            String scheme = getRequestingScheme();
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   160
            System.out.println("getRequestingScheme() returns " + scheme);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            String prompt = getRequestingPrompt();
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   162
            System.out.println("getRequestingPrompt() returns " + prompt);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   164
            if (!scheme.equals(expected [count][0])) {
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   165
                B4722333.except("wrong scheme received, " + scheme + " expected " + expected [count][0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   167
            if (!prompt.equals(expected [count][1])) {
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   168
                B4722333.except("wrong realm received, " + prompt + " expected " + expected [count][1]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            count ++;
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   171
            return (new PasswordAuthentication("user", "passwordNotCheckedAnyway".toCharArray()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public int getCount () {
55031
37a077319427 8224603: Replace wildcard address with loopback or local host in tests - part 11
dfuchs
parents: 54681
diff changeset
   175
            return count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}