test/jdk/sun/net/www/protocol/http/NoNTLM.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 47216 71c04702a3d5
child 55522 b2cbe2f674f0
permissions -rw-r--r--
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     1
/*
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     4
 *
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     8
 *
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    13
 * accompanied this code).
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    14
 *
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    18
 *
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    21
 * questions.
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    22
 */
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    23
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    24
/* @test
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    25
 * @bug 8004502
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    26
 * @library /test/lib
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    27
 * @summary Sanity check that NTLM will not be selected by the http protocol
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    28
 *    handler when running on a profile that does not support NTLM
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 15685
diff changeset
    29
 * @modules java.base/sun.net.www
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41965
diff changeset
    30
 *          java.base/sun.net.www.protocol.http:open
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    31
 * @run main/othervm NoNTLM
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    32
 */
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    33
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    34
import java.io.IOException;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    35
import java.lang.reflect.Field;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    36
import java.net.Authenticator;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    37
import java.net.HttpURLConnection;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    38
import java.net.InetAddress;
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    39
import java.net.PasswordAuthentication;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    40
import java.net.Proxy;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    41
import java.net.ServerSocket;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    42
import java.net.Socket;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
    43
import java.net.URL;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    44
import jdk.test.lib.net.URIBuilder;
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    45
import sun.net.www.MessageHeader;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    46
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    47
public class NoNTLM {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    48
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    49
    static final String CRLF = "\r\n";
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    50
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    51
    static final String OKAY =
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    52
        "HTTP/1.1 200" + CRLF +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    53
        "Content-Length: 0" + CRLF +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    54
        "Connection: close" + CRLF +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    55
        CRLF;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    56
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    57
    static class Client implements Runnable {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    58
        private final URL url;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    59
        private volatile IOException ioe;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    60
        private volatile int respCode;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    61
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    62
        Client(int port) throws IOException {
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    63
            this.url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    64
                .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    65
                .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    66
                .port(port)
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    67
                .path("/foo.html")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    68
                .toURLUnchecked();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    69
            System.out.println("Client URL: " + this.url);
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    70
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    71
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    72
        public void run() {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    73
            try {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    74
                HttpURLConnection uc =
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    75
                    (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    76
                try {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    77
                    uc.getInputStream();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    78
                } catch (IOException x) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    79
                    respCode = uc.getResponseCode();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    80
                    throw x;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    81
                }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    82
                uc.disconnect();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    83
            } catch (IOException x) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    84
                if (respCode == 0)
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    85
                    respCode = -1;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    86
                ioe = x;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    87
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    88
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    89
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    90
        IOException ioException() {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    91
            return ioe;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    92
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    93
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    94
        int respCode() {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    95
            return respCode;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    96
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    97
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    98
        static void start(int port) throws IOException {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
    99
            Client client = new Client(port);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   100
            new Thread(client).start();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   101
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   102
    }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   103
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   104
    /**
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   105
     * Return the http response with WWW-Authenticate headers for the given
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   106
     * authentication schemes.
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   107
     */
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   108
    static String authReplyFor(String... schemes) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   109
        // construct the server reply
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   110
        String reply = "HTTP/1.1 401 Unauthorized" + CRLF +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   111
                       "Content-Length: 0"+ CRLF +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   112
                       "Connection: close" + CRLF;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   113
        for (String s: schemes) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   114
            switch (s) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   115
                case "Basic" :
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   116
                    reply += "WWW-Authenticate: Basic realm=\"wallyworld\"" + CRLF;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   117
                    break;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   118
                case "Digest" :
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   119
                    reply += "WWW-Authenticate: Digest" +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   120
                             " realm=\"wallyworld\"" +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   121
                             " domain=/" +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   122
                             " nonce=\"abcdefghijklmnopqrstuvwxyz\"" +
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   123
                             " qop=\"auth\"" + CRLF;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   124
                    break;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   125
                case "NTLM" :
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   126
                    reply += "WWW-Authenticate: NTLM" + CRLF;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   127
                    break;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   128
                default :
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   129
                    throw new RuntimeException("Should not get here");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   130
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   131
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   132
        reply += CRLF;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   133
        return reply;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   134
    }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   135
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   136
    /**
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   137
     * Test the http protocol handler with the given authentication schemes
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   138
     * in the WWW-Authenticate header.
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   139
     */
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   140
    static void test(String... schemes) throws IOException {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   141
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   142
        // the authentication scheme that the client is expected to choose
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   143
        String expected = null;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   144
        for (String s: schemes) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   145
            if (expected == null) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   146
                expected = s;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   147
            } else if (s.equals("Digest")) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   148
                expected = s;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   149
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   150
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   151
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   152
        // server reply
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   153
        String reply = authReplyFor(schemes);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   154
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   155
        System.out.println("====================================");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   156
        System.out.println("Expect client to choose: " + expected);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   157
        System.out.println(reply);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   158
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   159
        try (ServerSocket ss = new ServerSocket(0)) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   160
            Client.start(ss.getLocalPort());
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   161
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   162
            // client ---- GET ---> server
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   163
            // client <--- 401 ---- server
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   164
            try (Socket s = ss.accept()) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   165
                new MessageHeader().parseHeader(s.getInputStream());
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   166
                s.getOutputStream().write(reply.getBytes("US-ASCII"));
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   167
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   168
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   169
            // client ---- GET ---> server
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   170
            // client <--- 200 ---- server
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   171
            String auth;
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   172
            try (Socket s = ss.accept()) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   173
                MessageHeader mh = new MessageHeader();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   174
                mh.parseHeader(s.getInputStream());
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   175
                s.getOutputStream().write(OKAY.getBytes("US-ASCII"));
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   176
                auth = mh.findValue("Authorization");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   177
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   178
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   179
            // check Authorization header
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   180
            if (auth == null)
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   181
                throw new RuntimeException("Authorization header not found");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   182
            System.out.println("Server received Authorization header: " + auth);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   183
            String[] values = auth.split(" ");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   184
            if (!values[0].equals(expected))
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   185
                throw new RuntimeException("Unexpected value");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   186
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   187
    }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   188
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   189
    /**
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   190
     * Test the http protocol handler with one WWW-Authenticate header with
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   191
     * the value "NTLM".
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   192
     */
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   193
    static void testNTLM() throws Exception {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   194
        // server reply
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   195
        String reply = authReplyFor("NTLM");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   196
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   197
        System.out.println("====================================");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   198
        System.out.println("Expect client to fail with 401 Unauthorized");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   199
        System.out.println(reply);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   200
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   201
        try (ServerSocket ss = new ServerSocket(0)) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   202
            Client client = new Client(ss.getLocalPort());
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   203
            Thread thr = new Thread(client);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   204
            thr.start();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   205
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   206
            // client ---- GET ---> server
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   207
            // client <--- 401 ---- client
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   208
            try (Socket s = ss.accept()) {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   209
                new MessageHeader().parseHeader(s.getInputStream());
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   210
                s.getOutputStream().write(reply.getBytes("US-ASCII"));
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   211
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   212
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   213
            // the client should fail with 401
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   214
            System.out.println("Waiting for client to terminate");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   215
            thr.join();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   216
            IOException ioe = client.ioException();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   217
            if (ioe != null)
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   218
                System.out.println("Client failed: " + ioe);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   219
            int respCode = client.respCode();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   220
            if (respCode != 0 && respCode != -1)
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   221
                System.out.println("Client received HTTP response code: " + respCode);
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   222
            if (respCode != HttpURLConnection.HTTP_UNAUTHORIZED)
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   223
                throw new RuntimeException("Unexpected response code");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   224
        }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   225
    }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   226
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   227
    public static void main(String[] args) throws Exception {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   228
        try {
41965
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   229
            Class<?> ntlmProxyClass = Class.forName("sun.net.www.protocol.http.NTLMAuthenticationProxy", true, NoNTLM.class.getClassLoader());
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   230
            Field ntlmSupportedField = ntlmProxyClass.getDeclaredField("supported");
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   231
            ntlmSupportedField.setAccessible(true);
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   232
            if (ntlmSupportedField.getBoolean(null)) {
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   233
                System.out.println("NTLM is supported. Nothing to do. Exiting.");
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   234
                return;
015dea372cb3 8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents: 30820
diff changeset
   235
            }
15685
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   236
        } catch (ClassNotFoundException okay) { }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   237
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   238
        // setup Authenticator
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   239
        Authenticator.setDefault(new Authenticator() {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   240
            @Override
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   241
            protected PasswordAuthentication getPasswordAuthentication() {
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   242
                return new PasswordAuthentication("user", "pass".toCharArray());
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   243
            }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   244
        });
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   245
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   246
        // test combinations of authentication schemes
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   247
        test("Basic");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   248
        test("Digest");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   249
        test("Basic", "Digest");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   250
        test("Basic", "NTLM");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   251
        test("Digest", "NTLM");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   252
        test("Basic", "Digest", "NTLM");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   253
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   254
        // test NTLM only, this should fail with "401 Unauthorized"
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   255
        testNTLM();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   256
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   257
        System.out.println();
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   258
        System.out.println("TEST PASSED");
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   259
    }
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   260
}
14c6889b6132 8004502: Compact Profiles contents
alanb
parents:
diff changeset
   261