Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java
41152 views
1
/*
2
* Copyright (c) 2016, 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
import java.io.IOException;
25
import java.net.Authenticator;
26
import java.net.HttpURLConnection;
27
import java.net.Proxy;
28
import java.net.URL;
29
import java.util.Arrays;
30
import java.util.stream.Collectors;
31
import java.util.stream.Stream;
32
33
/*
34
* @test
35
* @bug 8169415
36
* @library /test/lib
37
* @modules java.logging
38
* java.base/sun.net.www
39
* java.base/sun.net.www.protocol.http
40
* jdk.httpserver/sun.net.httpserver
41
* @build jdk.test.lib.net.SimpleSSLContext HTTPTest HTTPTestServer HTTPTestClient HTTPSetAuthenticatorTest
42
* @summary A simple HTTP test that starts an echo server supporting the given
43
* authentication scheme, then starts a regular HTTP client to invoke it.
44
* The client first does a GET request on "/", then follows on
45
* with a POST request that sends "Hello World!" to the server.
46
* The client expects to receive "Hello World!" in return.
47
* The test supports several execution modes:
48
* SERVER: The server performs Server authentication;
49
* PROXY: The server pretends to be a proxy and performs
50
* Proxy authentication;
51
* SERVER307: The server redirects the client (307) to another
52
* server that perform Server authentication;
53
* PROXY305: The server attempts to redirect
54
* the client to a proxy using 305 code;
55
* This test runs the client several times, providing different
56
* authenticators to the HttpURLConnection and verifies that
57
* the authenticator is invoked as expected - validating that
58
* connections with different authenticators do not share each
59
* other's socket channel and authentication info.
60
* Note: BASICSERVER means that the server will let the underlying
61
* com.sun.net.httpserver.HttpServer perform BASIC
62
* authentication when in Server mode. There should be
63
* no real difference between BASICSERVER and BASIC - it should
64
* be transparent on the client side.
65
* @run main/othervm HTTPSetAuthenticatorTest NONE SERVER PROXY SERVER307 PROXY305
66
* @run main/othervm HTTPSetAuthenticatorTest DIGEST SERVER
67
* @run main/othervm HTTPSetAuthenticatorTest DIGEST PROXY
68
* @run main/othervm HTTPSetAuthenticatorTest DIGEST PROXY305
69
* @run main/othervm HTTPSetAuthenticatorTest DIGEST SERVER307
70
* @run main/othervm HTTPSetAuthenticatorTest BASIC SERVER
71
* @run main/othervm HTTPSetAuthenticatorTest BASIC PROXY
72
* @run main/othervm HTTPSetAuthenticatorTest BASIC PROXY305
73
* @run main/othervm HTTPSetAuthenticatorTest BASIC SERVER307
74
* @run main/othervm HTTPSetAuthenticatorTest BASICSERVER SERVER
75
* @run main/othervm HTTPSetAuthenticatorTest BASICSERVER SERVER307
76
*
77
* @author danielfuchs
78
*/
79
public class HTTPSetAuthenticatorTest extends HTTPTest {
80
81
public static void main(String[] args) throws Exception {
82
String[] schemes;
83
String[] params;
84
if (args == null || args.length == 0) {
85
schemes = Stream.of(HttpSchemeType.values())
86
.map(HttpSchemeType::name)
87
.collect(Collectors.toList())
88
.toArray(new String[0]);
89
params = new String[0];
90
} else {
91
schemes = new String[] { args[0] };
92
params = Arrays.copyOfRange(args, 1, args.length);
93
}
94
for (String scheme : schemes) {
95
System.out.println("==== Testing with scheme=" + scheme + " ====\n");
96
new HTTPSetAuthenticatorTest(HttpSchemeType.valueOf(scheme))
97
.execute(params);
98
System.out.println();
99
}
100
}
101
102
final HttpSchemeType scheme;
103
public HTTPSetAuthenticatorTest(HttpSchemeType scheme) {
104
this.scheme = scheme;
105
}
106
107
@Override
108
public HttpSchemeType getHttpSchemeType() {
109
return scheme;
110
}
111
112
@Override
113
public int run(HTTPTestServer server,
114
HttpProtocolType protocol,
115
HttpAuthType mode)
116
throws IOException
117
{
118
HttpTestAuthenticator authOne = new HttpTestAuthenticator("authOne", "dublin", "foox");
119
HttpTestAuthenticator authTwo = new HttpTestAuthenticator("authTwo", "dublin", "foox");
120
int expectedIncrement = scheme == HttpSchemeType.NONE
121
? 0 : EXPECTED_AUTH_CALLS_PER_TEST;
122
int count;
123
int defaultCount = AUTHENTICATOR.count.get();
124
125
// Connect to the server with a GET request, then with a
126
// POST that contains "Hello World!"
127
// Uses authenticator #1
128
System.out.println("\nClient: Using authenticator #1: "
129
+ toString(authOne));
130
HTTPTestClient.connect(protocol, server, mode, authOne);
131
count = authOne.count.get();
132
if (count != expectedIncrement) {
133
throw new AssertionError("Authenticator #1 called " + count(count)
134
+ " expected it to be called " + expected(expectedIncrement));
135
}
136
137
// Connect to the server with a GET request, then with a
138
// POST that contains "Hello World!"
139
// Uses authenticator #2
140
System.out.println("\nClient: Using authenticator #2: "
141
+ toString(authTwo));
142
HTTPTestClient.connect(protocol, server, mode, authTwo);
143
count = authTwo.count.get();
144
if (count != expectedIncrement) {
145
throw new AssertionError("Authenticator #2 called " + count(count)
146
+ " expected it to be called " + expected(expectedIncrement));
147
}
148
149
// Connect to the server with a GET request, then with a
150
// POST that contains "Hello World!"
151
// Uses authenticator #1
152
System.out.println("\nClient: Using authenticator #1 again: "
153
+ toString(authOne));
154
HTTPTestClient.connect(protocol, server, mode, authOne);
155
count = authOne.count.get();
156
if (count != expectedIncrement) {
157
throw new AssertionError("Authenticator #1 called " + count(count)
158
+ " expected it to be called " + expected(expectedIncrement));
159
}
160
count = authTwo.count.get();
161
if (count != expectedIncrement) {
162
throw new AssertionError("Authenticator #2 called " + count(count)
163
+ " expected it to be called " + expected(expectedIncrement));
164
}
165
count = AUTHENTICATOR.count.get();
166
if (count != defaultCount) {
167
throw new AssertionError("Default Authenticator called " + count(count)
168
+ " expected it to be called " + expected(defaultCount));
169
}
170
171
// Now tries with the default authenticator: it should be invoked.
172
System.out.println("\nClient: Using the default authenticator: "
173
+ toString(null));
174
HTTPTestClient.connect(protocol, server, mode, null);
175
count = authOne.count.get();
176
if (count != expectedIncrement) {
177
throw new AssertionError("Authenticator #1 called " + count(count)
178
+ " expected it to be called " + expected(expectedIncrement));
179
}
180
count = authTwo.count.get();
181
if (count != expectedIncrement) {
182
throw new AssertionError("Authenticator #2 called " + count(count)
183
+ " expected it to be called " + expected(expectedIncrement));
184
}
185
count = AUTHENTICATOR.count.get();
186
if (count != defaultCount + expectedIncrement) {
187
throw new AssertionError("Default Authenticator called " + count(count)
188
+ " expected it to be called " + expected(defaultCount + expectedIncrement));
189
}
190
191
// Now tries with explicitly setting the default authenticator: it should
192
// be invoked again.
193
// Uncomment the code below when 8169068 is available.
194
// System.out.println("\nClient: Explicitly setting the default authenticator: "
195
// + toString(Authenticator.getDefault()));
196
// HTTPTestClient.connect(protocol, server, mode, Authenticator.getDefault());
197
// count = authOne.count.get();
198
// if (count != expectedIncrement) {
199
// throw new AssertionError("Authenticator #1 called " + count(count)
200
// + " expected it to be called " + expected(expectedIncrement));
201
// }
202
// count = authTwo.count.get();
203
// if (count != expectedIncrement) {
204
// throw new AssertionError("Authenticator #2 called " + count(count)
205
// + " expected it to be called " + expected(expectedIncrement));
206
// }
207
// count = AUTHENTICATOR.count.get();
208
// if (count != defaultCount + 2 * expectedIncrement) {
209
// throw new AssertionError("Default Authenticator called " + count(count)
210
// + " expected it to be called "
211
// + expected(defaultCount + 2 * expectedIncrement));
212
// }
213
214
// Now tries to set an authenticator on a connected connection.
215
URL url = url(protocol, server.getAddress(), "/");
216
Proxy proxy = proxy(server, mode);
217
HttpURLConnection conn = openConnection(url, mode, proxy);
218
try {
219
conn.setAuthenticator(null);
220
throw new RuntimeException("Expected NullPointerException"
221
+ " trying to set a null authenticator"
222
+ " not raised.");
223
} catch (NullPointerException npe) {
224
System.out.println("Client: caught expected NPE"
225
+ " trying to set a null authenticator: "
226
+ npe);
227
}
228
conn.connect();
229
try {
230
try {
231
conn.setAuthenticator(authOne);
232
throw new RuntimeException("Expected IllegalStateException"
233
+ " trying to set an authenticator after connect"
234
+ " not raised.");
235
} catch (IllegalStateException ise) {
236
System.out.println("Client: caught expected ISE"
237
+ " trying to set an authenticator after connect: "
238
+ ise);
239
}
240
// Uncomment the code below when 8169068 is available.
241
// try {
242
// conn.setAuthenticator(Authenticator.getDefault());
243
// throw new RuntimeException("Expected IllegalStateException"
244
// + " trying to set an authenticator after connect"
245
// + " not raised.");
246
// } catch (IllegalStateException ise) {
247
// System.out.println("Client: caught expected ISE"
248
// + " trying to set an authenticator after connect: "
249
// + ise);
250
// }
251
try {
252
conn.setAuthenticator(null);
253
throw new RuntimeException("Expected"
254
+ " IllegalStateException or NullPointerException"
255
+ " trying to set a null authenticator after connect"
256
+ " not raised.");
257
} catch (IllegalStateException | NullPointerException xxe) {
258
System.out.println("Client: caught expected "
259
+ xxe.getClass().getSimpleName()
260
+ " trying to set a null authenticator after connect: "
261
+ xxe);
262
}
263
} finally {
264
conn.disconnect();
265
}
266
267
// double check that authOne and authTwo haven't been invoked.
268
count = authOne.count.get();
269
if (count != expectedIncrement) {
270
throw new AssertionError("Authenticator #1 called " + count(count)
271
+ " expected it to be called " + expected(expectedIncrement));
272
}
273
count = authTwo.count.get();
274
if (count != expectedIncrement) {
275
throw new AssertionError("Authenticator #2 called " + count(count)
276
+ " expected it to be called " + expected(expectedIncrement));
277
}
278
279
// All good!
280
// return the number of times the default authenticator is supposed
281
// to have been called.
282
return scheme == HttpSchemeType.NONE ? 0 : 1 * EXPECTED_AUTH_CALLS_PER_TEST;
283
}
284
285
static String toString(Authenticator a) {
286
return sun.net.www.protocol.http.AuthenticatorKeys.getKey(a);
287
}
288
289
}
290
291