blob: 0742b0931c09da20cfdf6797a4ce1f1d5628400f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6520665 6357133
27 * @run main/othervm NTLMTest
28 * @summary 6520665 & 6357133: NTLM authentication issues.
29 */
30
31import java.net.*;
32import java.io.*;
33import sun.net.www.MessageHeader;
34
35public class NTLMTest
36{
37 public static void main(String[] args) {
38 Authenticator.setDefault(new NullAuthenticator());
39
40 try {
41 // Test with direct connection.
42 ServerSocket serverSS = new ServerSocket(0);
43 startServer(serverSS, false);
44 runClient(Proxy.NO_PROXY, serverSS.getLocalPort());
45
46 // Test with proxy.
47 serverSS = new ServerSocket(0);
48 startServer(serverSS, true /*proxy*/);
49 SocketAddress proxyAddr = new InetSocketAddress("localhost", serverSS.getLocalPort());
50 runClient(new Proxy(java.net.Proxy.Type.HTTP, proxyAddr), 8888);
51
52 } catch (IOException e) {
53 e.printStackTrace();
54 }
55 }
56
57 static void runClient(Proxy proxy, int serverPort) {
58 try {
59 String urlStr = "http://localhost:" + serverPort + "/";
60 URL url = new URL(urlStr);
61 HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
62 uc.getInputStream();
63
64 } catch (ProtocolException e) {
65 /* java.net.ProtocolException: Server redirected too many times (20) */
66 throw new RuntimeException("Failed: ProtocolException", e);
67 } catch (IOException ioe) {
68 /* IOException is OK. We are expecting "java.io.IOException: Server
69 * returned HTTP response code: 401 for URL: ..."
70 */
71 //ioe.printStackTrace();
72 } catch (NullPointerException npe) {
73 throw new RuntimeException("Failed: NPE thrown ", npe);
74 }
75 }
76
77 static String[] serverResp = new String[] {
78 "HTTP/1.1 401 Unauthorized\r\n" +
79 "Content-Length: 0\r\n" +
80 "WWW-Authenticate: NTLM\r\n\r\n",
81
82 "HTTP/1.1 401 Unauthorized\r\n" +
83 "Content-Length: 0\r\n" +
84 "WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};
85
86 static String[] proxyResp = new String[] {
87 "HTTP/1.1 407 Proxy Authentication Required\r\n" +
88 "Content-Length: 0\r\n" +
89 "Proxy-Authenticate: NTLM\r\n\r\n",
90
91 "HTTP/1.1 407 Proxy Authentication Required\r\n" +
92 "Content-Length: 0\r\n" +
93 "Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};
94
95 static void startServer(ServerSocket serverSS, boolean proxy) {
96 final ServerSocket ss = serverSS;
97 final boolean isProxy = proxy;
98
99 Thread thread = new Thread(new Runnable() {
100 public void run() {
101 boolean doing2ndStageNTLM = false;
102 while (true) {
103 try {
104 Socket s = ss.accept();
105 if (!doing2ndStageNTLM) {
106 handleConnection(s, isProxy ? proxyResp : serverResp, 0, 1);
107 doing2ndStageNTLM = true;
108 } else {
109 handleConnection(s, isProxy ? proxyResp : serverResp, 1, 2);
110 doing2ndStageNTLM = false;
111 }
112 connectionCount++;
113 //System.out.println("connectionCount = " + connectionCount);
114
115 } catch (IOException ioe) {
116 ioe.printStackTrace();
117 }
118 }
119 } });
120 thread.setDaemon(true);
121 thread.start();
122
123 }
124
125 static int connectionCount = 0;
126
127 static void handleConnection(Socket s, String[] resp, int start, int end) {
128 try {
129 OutputStream os = s.getOutputStream();
130
131 for (int i=start; i<end; i++) {
132 MessageHeader header = new MessageHeader (s.getInputStream());
133 //System.out.println("Input :" + header);
134 //System.out.println("Output:" + resp[i]);
135 os.write(resp[i].getBytes("ASCII"));
136 }
137
138 s.close();
139 } catch (IOException ioe) {
140 ioe.printStackTrace();
141 }
142 }
143
144 static class NullAuthenticator extends java.net.Authenticator
145 {
146 public int count = 0;
147
148 protected PasswordAuthentication getPasswordAuthentication() {
149 count++;
150 System.out.println("NullAuthenticator.getPasswordAuthentication called " + count + " times");
151
152 return null;
153 }
154
155 public int getCallCount() {
156 return count;
157 }
158 }
159
160}