Path: blob/master/test/jdk/sun/net/www/protocol/http/NTLMTest.java
41159 views
/*1* Copyright (c) 2007, 2019, 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/*24* @test25* @bug 6520665 635713326* @modules java.base/sun.net.www27* @library /test/lib28* @run main/othervm NTLMTest29* @summary 6520665 & 6357133: NTLM authentication issues.30*/3132import java.net.*;33import java.io.*;34import sun.net.www.MessageHeader;35import jdk.test.lib.net.URIBuilder;3637public class NTLMTest38{39public static void main(String[] args) throws Exception {40Authenticator.setDefault(new NullAuthenticator());4142try {43InetAddress loopback = InetAddress.getLoopbackAddress();4445// Test with direct connection.46try (NTLMServer server = startServer(new ServerSocket(0, 0, loopback), false)) {47runClient(Proxy.NO_PROXY, server.getLocalPort());48}49// Test with proxy.50try (NTLMServer server =51startServer(new ServerSocket(0, 0, loopback), true /*proxy*/)) {52SocketAddress proxyAddr = new InetSocketAddress(loopback, server.getLocalPort());53runClient(new Proxy(java.net.Proxy.Type.HTTP, proxyAddr), 8888);54}55} catch (IOException e) {56throw e;57}58}5960static void runClient(Proxy proxy, int serverPort) {61try {62URL url = URIBuilder.newBuilder()63.scheme("http")64.loopback()65.port(serverPort)66.path("/")67.toURLUnchecked();68HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);69uc.getInputStream().readAllBytes();7071} catch (ProtocolException e) {72/* java.net.ProtocolException: Server redirected too many times (20) */73throw new RuntimeException("Failed: ProtocolException", e);74} catch (IOException ioe) {75/* IOException is OK. We are expecting "java.io.IOException: Server76* returned HTTP response code: 401 for URL: ..."77*/78//ioe.printStackTrace();79System.out.println("Got expected " + ioe);80} catch (NullPointerException npe) {81throw new RuntimeException("Failed: NPE thrown ", npe);82}83}8485static String[] serverResp = new String[] {86"HTTP/1.1 401 Unauthorized\r\n" +87"Content-Length: 0\r\n" +88"WWW-Authenticate: NTLM\r\n\r\n",8990"HTTP/1.1 401 Unauthorized\r\n" +91"Content-Length: 0\r\n" +92"WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};9394static String[] proxyResp = new String[] {95"HTTP/1.1 407 Proxy Authentication Required\r\n" +96"Content-Length: 0\r\n" +97"Proxy-Authenticate: NTLM\r\n\r\n",9899"HTTP/1.1 407 Proxy Authentication Required\r\n" +100"Content-Length: 0\r\n" +101"Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};102103static class NTLMServer extends Thread implements AutoCloseable {104final ServerSocket ss;105final boolean isProxy;106volatile boolean closed;107108NTLMServer(ServerSocket serverSS, boolean proxy) {109super();110setDaemon(true);111ss = serverSS;112isProxy = proxy;113}114115public int getLocalPort() { return ss.getLocalPort(); }116117@Override118public void run() {119boolean doing2ndStageNTLM = false;120while (!closed) {121try {122Socket s = ss.accept();123if (!doing2ndStageNTLM) {124handleConnection(s, isProxy ? proxyResp : serverResp, 0, 1);125doing2ndStageNTLM = true;126} else {127handleConnection(s, isProxy ? proxyResp : serverResp, 1, 2);128doing2ndStageNTLM = false;129}130connectionCount++;131//System.out.println("connectionCount = " + connectionCount);132} catch (IOException ioe) {133if (!closed) ioe.printStackTrace();134}135}136}137138@Override139public void close() {140if (closed) return;141synchronized(this) {142if (closed) return;143closed = true;144}145try { ss.close(); } catch (IOException x) { };146}147}148149public static NTLMServer startServer(ServerSocket serverSS, boolean proxy) {150NTLMServer server = new NTLMServer(serverSS, proxy);151server.start();152return server;153}154155static int connectionCount = 0;156157static void handleConnection(Socket s, String[] resp, int start, int end) {158try {159OutputStream os = s.getOutputStream();160161for (int i=start; i<end; i++) {162MessageHeader header = new MessageHeader (s.getInputStream());163//System.out.println("Input :" + header);164//System.out.println("Output:" + resp[i]);165os.write(resp[i].getBytes("ASCII"));166}167168s.close();169} catch (IOException ioe) {170ioe.printStackTrace();171}172}173174static class NullAuthenticator extends java.net.Authenticator175{176public int count = 0;177178protected PasswordAuthentication getPasswordAuthentication() {179count++;180System.out.println("NullAuthenticator.getPasswordAuthentication called " + count + " times");181182return null;183}184185public int getCallCount() {186return count;187}188}189190}191192193