Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/NTLMTest.java
41159 views
1
/*
2
* Copyright (c) 2007, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6520665 6357133
27
* @modules java.base/sun.net.www
28
* @library /test/lib
29
* @run main/othervm NTLMTest
30
* @summary 6520665 & 6357133: NTLM authentication issues.
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import sun.net.www.MessageHeader;
36
import jdk.test.lib.net.URIBuilder;
37
38
public class NTLMTest
39
{
40
public static void main(String[] args) throws Exception {
41
Authenticator.setDefault(new NullAuthenticator());
42
43
try {
44
InetAddress loopback = InetAddress.getLoopbackAddress();
45
46
// Test with direct connection.
47
try (NTLMServer server = startServer(new ServerSocket(0, 0, loopback), false)) {
48
runClient(Proxy.NO_PROXY, server.getLocalPort());
49
}
50
// Test with proxy.
51
try (NTLMServer server =
52
startServer(new ServerSocket(0, 0, loopback), true /*proxy*/)) {
53
SocketAddress proxyAddr = new InetSocketAddress(loopback, server.getLocalPort());
54
runClient(new Proxy(java.net.Proxy.Type.HTTP, proxyAddr), 8888);
55
}
56
} catch (IOException e) {
57
throw e;
58
}
59
}
60
61
static void runClient(Proxy proxy, int serverPort) {
62
try {
63
URL url = URIBuilder.newBuilder()
64
.scheme("http")
65
.loopback()
66
.port(serverPort)
67
.path("/")
68
.toURLUnchecked();
69
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
70
uc.getInputStream().readAllBytes();
71
72
} catch (ProtocolException e) {
73
/* java.net.ProtocolException: Server redirected too many times (20) */
74
throw new RuntimeException("Failed: ProtocolException", e);
75
} catch (IOException ioe) {
76
/* IOException is OK. We are expecting "java.io.IOException: Server
77
* returned HTTP response code: 401 for URL: ..."
78
*/
79
//ioe.printStackTrace();
80
System.out.println("Got expected " + ioe);
81
} catch (NullPointerException npe) {
82
throw new RuntimeException("Failed: NPE thrown ", npe);
83
}
84
}
85
86
static String[] serverResp = new String[] {
87
"HTTP/1.1 401 Unauthorized\r\n" +
88
"Content-Length: 0\r\n" +
89
"WWW-Authenticate: NTLM\r\n\r\n",
90
91
"HTTP/1.1 401 Unauthorized\r\n" +
92
"Content-Length: 0\r\n" +
93
"WWW-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};
94
95
static String[] proxyResp = new String[] {
96
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
97
"Content-Length: 0\r\n" +
98
"Proxy-Authenticate: NTLM\r\n\r\n",
99
100
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
101
"Content-Length: 0\r\n" +
102
"Proxy-Authenticate: NTLM TlRMTVNTUAACAAAAAAAAACgAAAABggAAU3J2Tm9uY2UAAAAAAAAAAA==\r\n\r\n"};
103
104
static class NTLMServer extends Thread implements AutoCloseable {
105
final ServerSocket ss;
106
final boolean isProxy;
107
volatile boolean closed;
108
109
NTLMServer(ServerSocket serverSS, boolean proxy) {
110
super();
111
setDaemon(true);
112
ss = serverSS;
113
isProxy = proxy;
114
}
115
116
public int getLocalPort() { return ss.getLocalPort(); }
117
118
@Override
119
public void run() {
120
boolean doing2ndStageNTLM = false;
121
while (!closed) {
122
try {
123
Socket s = ss.accept();
124
if (!doing2ndStageNTLM) {
125
handleConnection(s, isProxy ? proxyResp : serverResp, 0, 1);
126
doing2ndStageNTLM = true;
127
} else {
128
handleConnection(s, isProxy ? proxyResp : serverResp, 1, 2);
129
doing2ndStageNTLM = false;
130
}
131
connectionCount++;
132
//System.out.println("connectionCount = " + connectionCount);
133
} catch (IOException ioe) {
134
if (!closed) ioe.printStackTrace();
135
}
136
}
137
}
138
139
@Override
140
public void close() {
141
if (closed) return;
142
synchronized(this) {
143
if (closed) return;
144
closed = true;
145
}
146
try { ss.close(); } catch (IOException x) { };
147
}
148
}
149
150
public static NTLMServer startServer(ServerSocket serverSS, boolean proxy) {
151
NTLMServer server = new NTLMServer(serverSS, proxy);
152
server.start();
153
return server;
154
}
155
156
static int connectionCount = 0;
157
158
static void handleConnection(Socket s, String[] resp, int start, int end) {
159
try {
160
OutputStream os = s.getOutputStream();
161
162
for (int i=start; i<end; i++) {
163
MessageHeader header = new MessageHeader (s.getInputStream());
164
//System.out.println("Input :" + header);
165
//System.out.println("Output:" + resp[i]);
166
os.write(resp[i].getBytes("ASCII"));
167
}
168
169
s.close();
170
} catch (IOException ioe) {
171
ioe.printStackTrace();
172
}
173
}
174
175
static class NullAuthenticator extends java.net.Authenticator
176
{
177
public int count = 0;
178
179
protected PasswordAuthentication getPasswordAuthentication() {
180
count++;
181
System.out.println("NullAuthenticator.getPasswordAuthentication called " + count + " times");
182
183
return null;
184
}
185
186
public int getCallCount() {
187
return count;
188
}
189
}
190
191
}
192
193