test/jdk/java/net/Authenticator/B6870935.java
author dfuchs
Mon, 17 Jun 2019 20:03:34 +0100
changeset 55399 46049b8a5658
parent 54086 ccb4a50bee06
permissions -rw-r--r--
8225578: Replace wildcard address with loopback or local host in tests - part 16 Summary: Fixes java/net/Authenticator and java/net/CookieHandler to stop depending on the wildcard address, wherever possible. Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     1
/*
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     4
 *
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     8
 *
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    13
 * accompanied this code).
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    14
 *
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
    21
 * questions.
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    22
 */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    23
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    24
/**
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    25
 * @test
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    26
 * @bug 6870935
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 5506
diff changeset
    27
 * @modules java.base/sun.net.www
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    28
 * @run main/othervm -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
    29
 * @run main/othervm -Djava.net.preferIPv6Addresses=true
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
    30
 *                   -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    31
 */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    32
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    33
import java.io.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    34
import java.util.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    35
import java.net.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    36
import java.security.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    37
import sun.net.www.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    38
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    39
/* This is one simple test of the RFC2617 digest authentication behavior
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    40
 * It specifically tests that the client correctly checks the returned
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    41
 * Authentication-Info header field from the server and throws an exception
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    42
 * if the password is wrong
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    43
 */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    44
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    45
public class B6870935 {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    46
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    47
    static char[] passwd = "password".toCharArray();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    48
    static String username = "user";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    49
    static String nonce = "abcdefghijklmnopqrstuvwxyz";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    50
    static String realm = "wallyworld";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    51
    static String uri = "http://www.ibm.com";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    52
    static volatile boolean error = false;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    53
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    54
    static class DigestServer extends Thread {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    55
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    56
        ServerSocket s;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    57
        InputStream  is;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    58
        OutputStream os;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    59
        int port;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    60
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    61
        String reply1 = "HTTP/1.1 407 Proxy Authentication Required\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    62
            "Proxy-Authenticate: Digest realm=\""+realm+"\" domain=/ "+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    63
            "nonce=\""+nonce+"\" qop=\"auth\"\r\n\r\n";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    64
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    65
        String reply2 = "HTTP/1.1 200 OK\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    66
            "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    67
            "Server: Apache/1.3.14 (Unix)\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    68
            "Content-Type: text/html; charset=iso-8859-1\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    69
            "Transfer-encoding: chunked\r\n\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    70
            "B\r\nHelloWorld1\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    71
            "B\r\nHelloWorld2\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    72
            "B\r\nHelloWorld3\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    73
            "B\r\nHelloWorld4\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    74
            "B\r\nHelloWorld5\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    75
            "0\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    76
            "Proxy-Authentication-Info: ";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    77
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    78
        DigestServer (ServerSocket y) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    79
            s = y;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    80
            port = s.getLocalPort();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    81
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    82
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    83
        public void run () {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    84
            try {
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
    85
                System.out.println("Server started");
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    86
                Socket s1 = s.accept ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    87
                is = s1.getInputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    88
                os = s1.getOutputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    89
                is.read ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    90
                os.write (reply1.getBytes());
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
    91
                System.out.println("First response sent");
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    92
                Thread.sleep (2000);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    93
                s1.close ();
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
    94
                System.out.println("First connection closed");
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    95
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    96
                s1 = s.accept ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    97
                is = s1.getInputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    98
                os = s1.getOutputStream ();
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
    99
                // is.read ();
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   100
                // need to get the cnonce out of the response
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   101
                MessageHeader header = new MessageHeader (is);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   102
                String raw = header.findValue ("Proxy-Authorization");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   103
                HeaderParser parser = new HeaderParser (raw);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   104
                String cnonce = parser.findValue ("cnonce");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   105
                String cnstring = parser.findValue ("nc");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   106
                String clientrsp = parser.findValue ("response");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   107
                String expected = computeDigest(
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   108
                        true, username,passwd,realm,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   109
                        "GET", uri, nonce, cnonce, cnstring
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   110
                );
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   111
                if (!expected.equals(clientrsp)) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   112
                    s1.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   113
                    s.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   114
                    error = true;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   115
                    return;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   116
                }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   117
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   118
                String reply = reply2 + getAuthorization (
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   119
                        realm, false, uri, "GET", cnonce,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   120
                        cnstring, passwd, username
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   121
                ) +"\r\n";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   122
                os.write (reply.getBytes());
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   123
                System.out.println("Second response sent");
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   124
                Thread.sleep (2000);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   125
                s1.close ();
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   126
                System.out.println("Second connection closed");
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   127
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   128
            catch (Exception e) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   129
                System.out.println (e);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   130
                e.printStackTrace();
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   131
            } finally {
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   132
                System.out.println("Server finished");
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   133
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   134
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   135
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   136
        private String getAuthorization (String realm, boolean isRequest, String uri, String method, String cnonce, String cnstring, char[] password, String username) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   137
            String response;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   138
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   139
            try {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   140
                response = computeDigest(isRequest, username,passwd,realm,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   141
                                            method, uri, nonce, cnonce, cnstring);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   142
            } catch (NoSuchAlgorithmException ex) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   143
                return null;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   144
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   145
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   146
            String value = "Digest"
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   147
                            + " qop=\"auth"
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   148
                            + "\", cnonce=\"" + cnonce
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   149
                            + "\", rspauth=\"" + response
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   150
                            + "\", nc=\"" + cnstring + "\"";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   151
            return (value+ "\r\n");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   152
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   153
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   154
        private String computeDigest(
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   155
                            boolean isRequest, String userName, char[] password,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   156
                            String realm, String connMethod,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   157
                            String requestURI, String nonceString,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   158
                            String cnonce, String ncValue
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   159
                        ) throws NoSuchAlgorithmException
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   160
        {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   161
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   162
            String A1, HashA1;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   163
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   164
            MessageDigest md = MessageDigest.getInstance("MD5");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   165
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   166
            {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   167
                A1 = userName + ":" + realm + ":";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   168
                HashA1 = encode(A1, password, md);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   169
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   170
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   171
            String A2;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   172
            if (isRequest) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   173
                A2 = connMethod + ":" + requestURI;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   174
            } else {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   175
                A2 = ":" + requestURI;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   176
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   177
            String HashA2 = encode(A2, null, md);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   178
            String combo, finalHash;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   179
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   180
            { /* RRC2617 when qop=auth */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   181
                combo = HashA1+ ":" + nonceString + ":" + ncValue + ":" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   182
                            cnonce + ":auth:" +HashA2;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   183
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   184
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   185
            finalHash = encode(combo, null, md);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   186
            return finalHash;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   187
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   188
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   189
        private final static char charArray[] = {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   190
            '0', '1', '2', '3', '4', '5', '6', '7',
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   191
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   192
        };
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   193
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   194
        private String encode(String src, char[] passwd, MessageDigest md) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   195
            md.update(src.getBytes());
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   196
            if (passwd != null) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   197
                byte[] passwdBytes = new byte[passwd.length];
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   198
                for (int i=0; i<passwd.length; i++)
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   199
                    passwdBytes[i] = (byte)passwd[i];
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   200
                md.update(passwdBytes);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   201
                Arrays.fill(passwdBytes, (byte)0x00);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   202
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   203
            byte[] digest = md.digest();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   204
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   205
            StringBuffer res = new StringBuffer(digest.length * 2);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   206
            for (int i = 0; i < digest.length; i++) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   207
                int hashchar = ((digest[i] >>> 4) & 0xf);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   208
                res.append(charArray[hashchar]);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   209
                hashchar = (digest[i] & 0xf);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   210
                res.append(charArray[hashchar]);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   211
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   212
            return res.toString();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   213
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   214
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   215
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   216
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   217
    static class MyAuthenticator extends Authenticator {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   218
        public MyAuthenticator () {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   219
            super ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   220
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   221
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   222
        public PasswordAuthentication getPasswordAuthentication ()
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   223
        {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   224
            return (new PasswordAuthentication (username, passwd));
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   225
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   226
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   227
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   228
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   229
    public static void main(String[] args) throws Exception {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   230
        int nLoops = 1;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   231
        int nSize = 10;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   232
        int port, n =0;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   233
        byte b[] = new byte[nSize];
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   234
        DigestServer server;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   235
        ServerSocket sock;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   236
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   237
        InetAddress address = InetAddress.getLoopbackAddress();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   238
        InetAddress resolved = InetAddress.getByName(address.getHostName());
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   239
        System.out.println("Lookup: "
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   240
                            + address + " -> \"" + address.getHostName() + "\" -> "
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   241
                            + resolved);
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   242
        String proxyHost = address.equals(resolved)
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   243
            ? address.getHostName()
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   244
            : address.getHostAddress();
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   245
        try {
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   246
            sock = new ServerSocket();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   247
            sock.bind(new InetSocketAddress(address, 0));
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   248
            port = sock.getLocalPort ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   249
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   250
        catch (Exception e) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   251
            System.out.println ("Exception: " + e);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   252
            return;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   253
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   254
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   255
        server = new DigestServer(sock);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   256
        server.start ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   257
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   258
        try  {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   259
            Authenticator.setDefault (new MyAuthenticator ());
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   260
            SocketAddress addr = InetSocketAddress.createUnresolved(proxyHost, port);
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   261
            Proxy proxy = new Proxy (Proxy.Type.HTTP, addr);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   262
            String s = "http://www.ibm.com";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   263
            URL url = new URL(s);
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   264
            System.out.println("opening connection through proxy: " + addr);
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   265
            java.net.URLConnection conURL =  url.openConnection(proxy);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   266
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   267
            InputStream in = conURL.getInputStream();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   268
            int c;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   269
            while ((c = in.read ()) != -1) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   270
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   271
            in.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   272
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   273
        catch(IOException e) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   274
            e.printStackTrace();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   275
            error = true;
55399
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   276
            sock.close();
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   277
        } finally {
46049b8a5658 8225578: Replace wildcard address with loopback or local host in tests - part 16
dfuchs
parents: 54086
diff changeset
   278
            server.join();
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   279
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   280
        if (error) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   281
            throw new RuntimeException ("Error in test");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   282
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   283
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   284
}