author | aeubanks |
Wed, 27 Mar 2019 09:06:43 -0700 | |
changeset 54314 | 46cf212cdcca |
parent 47216 | 71c04702a3d5 |
child 55522 | b2cbe2f674f0 |
permissions | -rw-r--r-- |
15685 | 1 |
/* |
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
2 |
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. |
15685 | 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 |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
26 |
* @library /test/lib |
15685 | 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 |
|
30820 | 29 |
* @modules java.base/sun.net.www |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41965
diff
changeset
|
30 |
* java.base/sun.net.www.protocol.http:open |
15685 | 31 |
* @run main/othervm NoNTLM |
32 |
*/ |
|
33 |
||
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
34 |
import java.io.IOException; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
35 |
import java.lang.reflect.Field; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
36 |
import java.net.Authenticator; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
37 |
import java.net.HttpURLConnection; |
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
38 |
import java.net.InetAddress; |
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
39 |
import java.net.PasswordAuthentication; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
40 |
import java.net.Proxy; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
41 |
import java.net.ServerSocket; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
42 |
import java.net.Socket; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
43 |
import java.net.URL; |
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
44 |
import jdk.test.lib.net.URIBuilder; |
15685 | 45 |
import sun.net.www.MessageHeader; |
46 |
||
47 |
public class NoNTLM { |
|
48 |
||
49 |
static final String CRLF = "\r\n"; |
|
50 |
||
51 |
static final String OKAY = |
|
52 |
"HTTP/1.1 200" + CRLF + |
|
53 |
"Content-Length: 0" + CRLF + |
|
54 |
"Connection: close" + CRLF + |
|
55 |
CRLF; |
|
56 |
||
57 |
static class Client implements Runnable { |
|
58 |
private final URL url; |
|
59 |
private volatile IOException ioe; |
|
60 |
private volatile int respCode; |
|
61 |
||
62 |
Client(int port) throws IOException { |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
63 |
this.url = URIBuilder.newBuilder() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
64 |
.scheme("http") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
65 |
.loopback() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
66 |
.port(port) |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
67 |
.path("/foo.html") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
68 |
.toURLUnchecked(); |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
69 |
System.out.println("Client URL: " + this.url); |
15685 | 70 |
} |
71 |
||
72 |
public void run() { |
|
73 |
try { |
|
74 |
HttpURLConnection uc = |
|
75 |
(HttpURLConnection)url.openConnection(Proxy.NO_PROXY); |
|
76 |
try { |
|
77 |
uc.getInputStream(); |
|
78 |
} catch (IOException x) { |
|
79 |
respCode = uc.getResponseCode(); |
|
80 |
throw x; |
|
81 |
} |
|
82 |
uc.disconnect(); |
|
83 |
} catch (IOException x) { |
|
84 |
if (respCode == 0) |
|
85 |
respCode = -1; |
|
86 |
ioe = x; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
IOException ioException() { |
|
91 |
return ioe; |
|
92 |
} |
|
93 |
||
94 |
int respCode() { |
|
95 |
return respCode; |
|
96 |
} |
|
97 |
||
98 |
static void start(int port) throws IOException { |
|
99 |
Client client = new Client(port); |
|
100 |
new Thread(client).start(); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Return the http response with WWW-Authenticate headers for the given |
|
106 |
* authentication schemes. |
|
107 |
*/ |
|
108 |
static String authReplyFor(String... schemes) { |
|
109 |
// construct the server reply |
|
110 |
String reply = "HTTP/1.1 401 Unauthorized" + CRLF + |
|
111 |
"Content-Length: 0"+ CRLF + |
|
112 |
"Connection: close" + CRLF; |
|
113 |
for (String s: schemes) { |
|
114 |
switch (s) { |
|
115 |
case "Basic" : |
|
116 |
reply += "WWW-Authenticate: Basic realm=\"wallyworld\"" + CRLF; |
|
117 |
break; |
|
118 |
case "Digest" : |
|
119 |
reply += "WWW-Authenticate: Digest" + |
|
120 |
" realm=\"wallyworld\"" + |
|
121 |
" domain=/" + |
|
122 |
" nonce=\"abcdefghijklmnopqrstuvwxyz\"" + |
|
123 |
" qop=\"auth\"" + CRLF; |
|
124 |
break; |
|
125 |
case "NTLM" : |
|
126 |
reply += "WWW-Authenticate: NTLM" + CRLF; |
|
127 |
break; |
|
128 |
default : |
|
129 |
throw new RuntimeException("Should not get here"); |
|
130 |
} |
|
131 |
} |
|
132 |
reply += CRLF; |
|
133 |
return reply; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Test the http protocol handler with the given authentication schemes |
|
138 |
* in the WWW-Authenticate header. |
|
139 |
*/ |
|
140 |
static void test(String... schemes) throws IOException { |
|
141 |
||
142 |
// the authentication scheme that the client is expected to choose |
|
143 |
String expected = null; |
|
144 |
for (String s: schemes) { |
|
145 |
if (expected == null) { |
|
146 |
expected = s; |
|
147 |
} else if (s.equals("Digest")) { |
|
148 |
expected = s; |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
// server reply |
|
153 |
String reply = authReplyFor(schemes); |
|
154 |
||
155 |
System.out.println("===================================="); |
|
156 |
System.out.println("Expect client to choose: " + expected); |
|
157 |
System.out.println(reply); |
|
158 |
||
159 |
try (ServerSocket ss = new ServerSocket(0)) { |
|
160 |
Client.start(ss.getLocalPort()); |
|
161 |
||
162 |
// client ---- GET ---> server |
|
163 |
// client <--- 401 ---- server |
|
164 |
try (Socket s = ss.accept()) { |
|
165 |
new MessageHeader().parseHeader(s.getInputStream()); |
|
166 |
s.getOutputStream().write(reply.getBytes("US-ASCII")); |
|
167 |
} |
|
168 |
||
169 |
// client ---- GET ---> server |
|
170 |
// client <--- 200 ---- server |
|
171 |
String auth; |
|
172 |
try (Socket s = ss.accept()) { |
|
173 |
MessageHeader mh = new MessageHeader(); |
|
174 |
mh.parseHeader(s.getInputStream()); |
|
175 |
s.getOutputStream().write(OKAY.getBytes("US-ASCII")); |
|
176 |
auth = mh.findValue("Authorization"); |
|
177 |
} |
|
178 |
||
179 |
// check Authorization header |
|
180 |
if (auth == null) |
|
181 |
throw new RuntimeException("Authorization header not found"); |
|
182 |
System.out.println("Server received Authorization header: " + auth); |
|
183 |
String[] values = auth.split(" "); |
|
184 |
if (!values[0].equals(expected)) |
|
185 |
throw new RuntimeException("Unexpected value"); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Test the http protocol handler with one WWW-Authenticate header with |
|
191 |
* the value "NTLM". |
|
192 |
*/ |
|
193 |
static void testNTLM() throws Exception { |
|
194 |
// server reply |
|
195 |
String reply = authReplyFor("NTLM"); |
|
196 |
||
197 |
System.out.println("===================================="); |
|
198 |
System.out.println("Expect client to fail with 401 Unauthorized"); |
|
199 |
System.out.println(reply); |
|
200 |
||
201 |
try (ServerSocket ss = new ServerSocket(0)) { |
|
202 |
Client client = new Client(ss.getLocalPort()); |
|
203 |
Thread thr = new Thread(client); |
|
204 |
thr.start(); |
|
205 |
||
206 |
// client ---- GET ---> server |
|
207 |
// client <--- 401 ---- client |
|
208 |
try (Socket s = ss.accept()) { |
|
209 |
new MessageHeader().parseHeader(s.getInputStream()); |
|
210 |
s.getOutputStream().write(reply.getBytes("US-ASCII")); |
|
211 |
} |
|
212 |
||
213 |
// the client should fail with 401 |
|
214 |
System.out.println("Waiting for client to terminate"); |
|
215 |
thr.join(); |
|
216 |
IOException ioe = client.ioException(); |
|
217 |
if (ioe != null) |
|
218 |
System.out.println("Client failed: " + ioe); |
|
219 |
int respCode = client.respCode(); |
|
220 |
if (respCode != 0 && respCode != -1) |
|
221 |
System.out.println("Client received HTTP response code: " + respCode); |
|
222 |
if (respCode != HttpURLConnection.HTTP_UNAUTHORIZED) |
|
223 |
throw new RuntimeException("Unexpected response code"); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
public static void main(String[] args) throws Exception { |
|
228 |
try { |
|
41965
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
229 |
Class<?> ntlmProxyClass = Class.forName("sun.net.www.protocol.http.NTLMAuthenticationProxy", true, NoNTLM.class.getClassLoader()); |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
230 |
Field ntlmSupportedField = ntlmProxyClass.getDeclaredField("supported"); |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
231 |
ntlmSupportedField.setAccessible(true); |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
232 |
if (ntlmSupportedField.getBoolean(null)) { |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
233 |
System.out.println("NTLM is supported. Nothing to do. Exiting."); |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
234 |
return; |
015dea372cb3
8169196: [TESTBUG] Three tests from sun/net/www have undeclared dependencies
skovalev
parents:
30820
diff
changeset
|
235 |
} |
15685 | 236 |
} catch (ClassNotFoundException okay) { } |
237 |
||
238 |
// setup Authenticator |
|
239 |
Authenticator.setDefault(new Authenticator() { |
|
240 |
@Override |
|
241 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
242 |
return new PasswordAuthentication("user", "pass".toCharArray()); |
|
243 |
} |
|
244 |
}); |
|
245 |
||
246 |
// test combinations of authentication schemes |
|
247 |
test("Basic"); |
|
248 |
test("Digest"); |
|
249 |
test("Basic", "Digest"); |
|
250 |
test("Basic", "NTLM"); |
|
251 |
test("Digest", "NTLM"); |
|
252 |
test("Basic", "Digest", "NTLM"); |
|
253 |
||
254 |
// test NTLM only, this should fail with "401 Unauthorized" |
|
255 |
testNTLM(); |
|
256 |
||
257 |
System.out.println(); |
|
258 |
System.out.println("TEST PASSED"); |
|
259 |
} |
|
260 |
} |
|
261 |