Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/NTLMTest.java
38867 views
/*1* Copyright (c) 2007, 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* @run main/othervm NTLMTest27* @summary 6520665 & 6357133: NTLM authentication issues.28*/2930import java.net.*;31import java.io.*;32import sun.net.www.MessageHeader;3334public class NTLMTest35{36public static void main(String[] args) {37Authenticator.setDefault(new NullAuthenticator());3839try {40// Test with direct connection.41ServerSocket serverSS = new ServerSocket(0);42startServer(serverSS, false);43runClient(Proxy.NO_PROXY, serverSS.getLocalPort());4445// Test with proxy.46serverSS = new ServerSocket(0);47startServer(serverSS, true /*proxy*/);48SocketAddress proxyAddr = new InetSocketAddress("localhost", serverSS.getLocalPort());49runClient(new Proxy(java.net.Proxy.Type.HTTP, proxyAddr), 8888);5051} catch (IOException e) {52e.printStackTrace();53}54}5556static void runClient(Proxy proxy, int serverPort) {57try {58String urlStr = "http://localhost:" + serverPort + "/";59URL url = new URL(urlStr);60HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);61uc.getInputStream();6263} catch (ProtocolException e) {64/* java.net.ProtocolException: Server redirected too many times (20) */65throw new RuntimeException("Failed: ProtocolException", e);66} catch (IOException ioe) {67/* IOException is OK. We are expecting "java.io.IOException: Server68* returned HTTP response code: 401 for URL: ..."69*/70//ioe.printStackTrace();71} catch (NullPointerException npe) {72throw new RuntimeException("Failed: NPE thrown ", npe);73}74}7576static String[] serverResp = new String[] {77"HTTP/1.1 401 Unauthorized\r\n" +78"Content-Length: 0\r\n" +79"WWW-Authenticate: NTLM\r\n\r\n",8081"HTTP/1.1 401 Unauthorized\r\n" +82"Content-Length: 0\r\n" +83"WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};8485static String[] proxyResp = new String[] {86"HTTP/1.1 407 Proxy Authentication Required\r\n" +87"Content-Length: 0\r\n" +88"Proxy-Authenticate: NTLM\r\n\r\n",8990"HTTP/1.1 407 Proxy Authentication Required\r\n" +91"Content-Length: 0\r\n" +92"Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};9394static void startServer(ServerSocket serverSS, boolean proxy) {95final ServerSocket ss = serverSS;96final boolean isProxy = proxy;9798Thread thread = new Thread(new Runnable() {99public void run() {100boolean doing2ndStageNTLM = false;101while (true) {102try {103Socket s = ss.accept();104if (!doing2ndStageNTLM) {105handleConnection(s, isProxy ? proxyResp : serverResp, 0, 1);106doing2ndStageNTLM = true;107} else {108handleConnection(s, isProxy ? proxyResp : serverResp, 1, 2);109doing2ndStageNTLM = false;110}111connectionCount++;112//System.out.println("connectionCount = " + connectionCount);113114} catch (IOException ioe) {115ioe.printStackTrace();116}117}118} });119thread.setDaemon(true);120thread.start();121122}123124static int connectionCount = 0;125126static void handleConnection(Socket s, String[] resp, int start, int end) {127try {128OutputStream os = s.getOutputStream();129130for (int i=start; i<end; i++) {131MessageHeader header = new MessageHeader (s.getInputStream());132//System.out.println("Input :" + header);133//System.out.println("Output:" + resp[i]);134os.write(resp[i].getBytes("ASCII"));135}136137s.close();138} catch (IOException ioe) {139ioe.printStackTrace();140}141}142143static class NullAuthenticator extends java.net.Authenticator144{145public int count = 0;146147protected PasswordAuthentication getPasswordAuthentication() {148count++;149System.out.println("NullAuthenticator.getPasswordAuthentication called " + count + " times");150151return null;152}153154public int getCallCount() {155return count;156}157}158159}160161162