Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/NoNTLM.java
38867 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 800450225* @summary Sanity check that NTLM will not be selected by the http protocol26* handler when running on a profile that does not support NTLM27* @run main/othervm NoNTLM28*/2930import java.net.*;31import java.io.*;32import sun.net.www.MessageHeader;3334public class NoNTLM {3536static final String CRLF = "\r\n";3738static final String OKAY =39"HTTP/1.1 200" + CRLF +40"Content-Length: 0" + CRLF +41"Connection: close" + CRLF +42CRLF;4344static class Client implements Runnable {45private final URL url;46private volatile IOException ioe;47private volatile int respCode;4849Client(int port) throws IOException {50this.url = new URL("http://127.0.0.1:" + port + "/foo.html");51}5253public void run() {54try {55HttpURLConnection uc =56(HttpURLConnection)url.openConnection(Proxy.NO_PROXY);57try {58uc.getInputStream();59} catch (IOException x) {60respCode = uc.getResponseCode();61throw x;62}63uc.disconnect();64} catch (IOException x) {65if (respCode == 0)66respCode = -1;67ioe = x;68}69}7071IOException ioException() {72return ioe;73}7475int respCode() {76return respCode;77}7879static void start(int port) throws IOException {80Client client = new Client(port);81new Thread(client).start();82}83}8485/**86* Return the http response with WWW-Authenticate headers for the given87* authentication schemes.88*/89static String authReplyFor(String... schemes) {90// construct the server reply91String reply = "HTTP/1.1 401 Unauthorized" + CRLF +92"Content-Length: 0"+ CRLF +93"Connection: close" + CRLF;94for (String s: schemes) {95switch (s) {96case "Basic" :97reply += "WWW-Authenticate: Basic realm=\"wallyworld\"" + CRLF;98break;99case "Digest" :100reply += "WWW-Authenticate: Digest" +101" realm=\"wallyworld\"" +102" domain=/" +103" nonce=\"abcdefghijklmnopqrstuvwxyz\"" +104" qop=\"auth\"" + CRLF;105break;106case "NTLM" :107reply += "WWW-Authenticate: NTLM" + CRLF;108break;109default :110throw new RuntimeException("Should not get here");111}112}113reply += CRLF;114return reply;115}116117/**118* Test the http protocol handler with the given authentication schemes119* in the WWW-Authenticate header.120*/121static void test(String... schemes) throws IOException {122123// the authentication scheme that the client is expected to choose124String expected = null;125for (String s: schemes) {126if (expected == null) {127expected = s;128} else if (s.equals("Digest")) {129expected = s;130}131}132133// server reply134String reply = authReplyFor(schemes);135136System.out.println("====================================");137System.out.println("Expect client to choose: " + expected);138System.out.println(reply);139140try (ServerSocket ss = new ServerSocket(0)) {141Client.start(ss.getLocalPort());142143// client ---- GET ---> server144// client <--- 401 ---- server145try (Socket s = ss.accept()) {146new MessageHeader().parseHeader(s.getInputStream());147s.getOutputStream().write(reply.getBytes("US-ASCII"));148}149150// client ---- GET ---> server151// client <--- 200 ---- server152String auth;153try (Socket s = ss.accept()) {154MessageHeader mh = new MessageHeader();155mh.parseHeader(s.getInputStream());156s.getOutputStream().write(OKAY.getBytes("US-ASCII"));157auth = mh.findValue("Authorization");158}159160// check Authorization header161if (auth == null)162throw new RuntimeException("Authorization header not found");163System.out.println("Server received Authorization header: " + auth);164String[] values = auth.split(" ");165if (!values[0].equals(expected))166throw new RuntimeException("Unexpected value");167}168}169170/**171* Test the http protocol handler with one WWW-Authenticate header with172* the value "NTLM".173*/174static void testNTLM() throws Exception {175// server reply176String reply = authReplyFor("NTLM");177178System.out.println("====================================");179System.out.println("Expect client to fail with 401 Unauthorized");180System.out.println(reply);181182try (ServerSocket ss = new ServerSocket(0)) {183Client client = new Client(ss.getLocalPort());184Thread thr = new Thread(client);185thr.start();186187// client ---- GET ---> server188// client <--- 401 ---- client189try (Socket s = ss.accept()) {190new MessageHeader().parseHeader(s.getInputStream());191s.getOutputStream().write(reply.getBytes("US-ASCII"));192}193194// the client should fail with 401195System.out.println("Waiting for client to terminate");196thr.join();197IOException ioe = client.ioException();198if (ioe != null)199System.out.println("Client failed: " + ioe);200int respCode = client.respCode();201if (respCode != 0 && respCode != -1)202System.out.println("Client received HTTP response code: " + respCode);203if (respCode != HttpURLConnection.HTTP_UNAUTHORIZED)204throw new RuntimeException("Unexpected response code");205}206}207208public static void main(String[] args) throws Exception {209// assume NTLM is not supported when Kerberos is not available210try {211Class.forName("javax.security.auth.kerberos.KerberosPrincipal");212System.out.println("Kerberos is present, assuming NTLM is supported too");213return;214} catch (ClassNotFoundException okay) { }215216// setup Authenticator217Authenticator.setDefault(new Authenticator() {218@Override219protected PasswordAuthentication getPasswordAuthentication() {220return new PasswordAuthentication("user", "pass".toCharArray());221}222});223224// test combinations of authentication schemes225test("Basic");226test("Digest");227test("Basic", "Digest");228test("Basic", "NTLM");229test("Digest", "NTLM");230test("Basic", "Digest", "NTLM");231232// test NTLM only, this should fail with "401 Unauthorized"233testNTLM();234235System.out.println();236System.out.println("TEST PASSED");237}238}239240241242