Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/NoNTLM.java
41159 views
1
/*
2
* Copyright (c) 2013, 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
/* @test
25
* @bug 8004502
26
* @library /test/lib
27
* @summary Sanity check that NTLM will not be selected by the http protocol
28
* handler when running on a profile that does not support NTLM
29
* @modules java.base/sun.net.www
30
* java.base/sun.net.www.protocol.http:open
31
* @run main/othervm NoNTLM
32
* @run main/othervm -Djava.net.preferIPv6Addresses=true NoNTLM
33
*/
34
35
import java.io.IOException;
36
import java.lang.reflect.Field;
37
import java.net.Authenticator;
38
import java.net.HttpURLConnection;
39
import java.net.InetAddress;
40
import java.net.PasswordAuthentication;
41
import java.net.Proxy;
42
import java.net.ServerSocket;
43
import java.net.Socket;
44
import java.net.URL;
45
import jdk.test.lib.net.URIBuilder;
46
import sun.net.www.MessageHeader;
47
48
public class NoNTLM {
49
50
static final String CRLF = "\r\n";
51
52
static final String OKAY =
53
"HTTP/1.1 200" + CRLF +
54
"Content-Length: 0" + CRLF +
55
"Connection: close" + CRLF +
56
CRLF;
57
58
static class Client implements Runnable {
59
private final URL url;
60
private volatile IOException ioe;
61
private volatile int respCode;
62
63
Client(int port) throws IOException {
64
this.url = URIBuilder.newBuilder()
65
.scheme("http")
66
.loopback()
67
.port(port)
68
.path("/foo.html")
69
.toURLUnchecked();
70
System.out.println("Client URL: " + this.url);
71
}
72
73
public void run() {
74
try {
75
HttpURLConnection uc =
76
(HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
77
try {
78
uc.getInputStream();
79
} catch (IOException x) {
80
respCode = uc.getResponseCode();
81
throw x;
82
}
83
uc.disconnect();
84
} catch (IOException x) {
85
if (respCode == 0)
86
respCode = -1;
87
ioe = x;
88
}
89
}
90
91
IOException ioException() {
92
return ioe;
93
}
94
95
int respCode() {
96
return respCode;
97
}
98
99
static void start(int port) throws IOException {
100
Client client = new Client(port);
101
new Thread(client).start();
102
}
103
}
104
105
/**
106
* Return the http response with WWW-Authenticate headers for the given
107
* authentication schemes.
108
*/
109
static String authReplyFor(String... schemes) {
110
// construct the server reply
111
String reply = "HTTP/1.1 401 Unauthorized" + CRLF +
112
"Content-Length: 0"+ CRLF +
113
"Connection: close" + CRLF;
114
for (String s: schemes) {
115
switch (s) {
116
case "Basic" :
117
reply += "WWW-Authenticate: Basic realm=\"wallyworld\"" + CRLF;
118
break;
119
case "Digest" :
120
reply += "WWW-Authenticate: Digest" +
121
" realm=\"wallyworld\"" +
122
" domain=/" +
123
" nonce=\"abcdefghijklmnopqrstuvwxyz\"" +
124
" qop=\"auth\"" + CRLF;
125
break;
126
case "NTLM" :
127
reply += "WWW-Authenticate: NTLM" + CRLF;
128
break;
129
default :
130
throw new RuntimeException("Should not get here");
131
}
132
}
133
reply += CRLF;
134
return reply;
135
}
136
137
/**
138
* Test the http protocol handler with the given authentication schemes
139
* in the WWW-Authenticate header.
140
*/
141
static void test(String... schemes) throws IOException {
142
143
// the authentication scheme that the client is expected to choose
144
String expected = null;
145
for (String s: schemes) {
146
if (expected == null) {
147
expected = s;
148
} else if (s.equals("Digest")) {
149
expected = s;
150
}
151
}
152
153
// server reply
154
String reply = authReplyFor(schemes);
155
156
System.out.println("====================================");
157
System.out.println("Expect client to choose: " + expected);
158
System.out.println(reply);
159
InetAddress loopback = InetAddress.getLoopbackAddress();
160
try (ServerSocket ss = new ServerSocket(0, 0, loopback)) {
161
Client.start(ss.getLocalPort());
162
163
// client ---- GET ---> server
164
// client <--- 401 ---- server
165
try (Socket s = ss.accept()) {
166
new MessageHeader().parseHeader(s.getInputStream());
167
s.getOutputStream().write(reply.getBytes("US-ASCII"));
168
}
169
170
// client ---- GET ---> server
171
// client <--- 200 ---- server
172
String auth;
173
try (Socket s = ss.accept()) {
174
MessageHeader mh = new MessageHeader();
175
mh.parseHeader(s.getInputStream());
176
s.getOutputStream().write(OKAY.getBytes("US-ASCII"));
177
auth = mh.findValue("Authorization");
178
}
179
180
// check Authorization header
181
if (auth == null)
182
throw new RuntimeException("Authorization header not found");
183
System.out.println("Server received Authorization header: " + auth);
184
String[] values = auth.split(" ");
185
if (!values[0].equals(expected))
186
throw new RuntimeException("Unexpected value");
187
}
188
}
189
190
/**
191
* Test the http protocol handler with one WWW-Authenticate header with
192
* the value "NTLM".
193
*/
194
static void testNTLM() throws Exception {
195
// server reply
196
String reply = authReplyFor("NTLM");
197
198
System.out.println("====================================");
199
System.out.println("Expect client to fail with 401 Unauthorized");
200
System.out.println(reply);
201
202
InetAddress loopback = InetAddress.getLoopbackAddress();
203
try (ServerSocket ss = new ServerSocket(0, 0, loopback)) {
204
Client client = new Client(ss.getLocalPort());
205
Thread thr = new Thread(client);
206
thr.start();
207
208
// client ---- GET ---> server
209
// client <--- 401 ---- client
210
try (Socket s = ss.accept()) {
211
new MessageHeader().parseHeader(s.getInputStream());
212
s.getOutputStream().write(reply.getBytes("US-ASCII"));
213
}
214
215
// the client should fail with 401
216
System.out.println("Waiting for client to terminate");
217
thr.join();
218
IOException ioe = client.ioException();
219
if (ioe != null)
220
System.out.println("Client failed: " + ioe);
221
int respCode = client.respCode();
222
if (respCode != 0 && respCode != -1)
223
System.out.println("Client received HTTP response code: " + respCode);
224
if (respCode != HttpURLConnection.HTTP_UNAUTHORIZED)
225
throw new RuntimeException("Unexpected response code");
226
}
227
}
228
229
public static void main(String[] args) throws Exception {
230
boolean ntlmSupported = false;
231
try {
232
Class<?> ntlmProxyClass = Class.forName("sun.net.www.protocol.http.NTLMAuthenticationProxy", true, NoNTLM.class.getClassLoader());
233
Field ntlmSupportedField = ntlmProxyClass.getDeclaredField("supported");
234
ntlmSupportedField.setAccessible(true);
235
if (ntlmSupportedField.getBoolean(null)) {
236
System.out.println("NTLM is supported.");
237
ntlmSupported = true;
238
}
239
} catch (ClassNotFoundException okay) { }
240
241
// setup Authenticator
242
Authenticator.setDefault(new Authenticator() {
243
@Override
244
protected PasswordAuthentication getPasswordAuthentication() {
245
return new PasswordAuthentication("user", "pass".toCharArray());
246
}
247
});
248
249
// test combinations of authentication schemes
250
test("Basic");
251
test("Digest");
252
test("Basic", "Digest");
253
254
if (ntlmSupported) {
255
System.out.println("====================================");
256
System.out.println("NTLM is supported: client would select NTLM: skipping `test(\"Basic\", \"NTLM\")`..");
257
} else {
258
test("Basic", "NTLM");
259
}
260
261
test("Digest", "NTLM");
262
test("Basic", "Digest", "NTLM");
263
264
if (ntlmSupported) {
265
System.out.println("====================================");
266
System.out.println("NTLM is supported: client would select NTLM: skipping `testNTLM()`..");
267
} else {
268
// test NTLM only, this should fail with "401 Unauthorized"
269
testNTLM();
270
}
271
272
System.out.println();
273
System.out.println("TEST PASSED");
274
}
275
}
276
277