Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B4933582.java
41149 views
1
/*
2
* Copyright (c) 2003, 2021, 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
// Note: this test saves a cache.ser file in the scratch directory,
25
// which the cache implementation will load its configuration
26
// from. Therefore adding several @run lines does not work.
27
28
/*
29
* @test
30
* @bug 4933582
31
* @key intermittent
32
* @library /test/lib
33
* @modules java.base/sun.net.www
34
* java.base/sun.net.www.protocol.http
35
*
36
* @run main/othervm B4933582
37
*/
38
39
import java.io.File;
40
import java.io.FileInputStream;
41
import java.io.FileOutputStream;
42
import java.io.IOException;
43
import java.io.InputStream;
44
import java.io.ObjectInputStream;
45
import java.io.ObjectOutputStream;
46
import java.io.PrintWriter;
47
import java.net.Authenticator;
48
import java.net.BindException;
49
import java.net.InetAddress;
50
import java.net.InetSocketAddress;
51
import java.net.PasswordAuthentication;
52
import java.net.ProxySelector;
53
import java.net.URL;
54
import java.net.URLConnection;
55
import java.util.HashMap;
56
import java.util.LinkedList;
57
import java.util.concurrent.Executors;
58
59
import com.sun.net.httpserver.HttpExchange;
60
import com.sun.net.httpserver.HttpHandler;
61
import com.sun.net.httpserver.HttpServer;
62
import jdk.test.lib.net.URIBuilder;
63
import sun.net.www.protocol.http.AuthCacheImpl;
64
import sun.net.www.protocol.http.AuthCacheValue;
65
66
public class B4933582 implements HttpHandler {
67
68
static int count = 0;
69
static String authstring;
70
71
void errorReply (HttpExchange req, String reply) throws IOException {
72
req.getResponseHeaders().set("Connection", "close");
73
req.getResponseHeaders().set("WWW-Authenticate", reply);
74
req.sendResponseHeaders(401, -1);
75
}
76
77
void okReply (HttpExchange req) throws IOException {
78
req.sendResponseHeaders(200, 0);
79
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
80
pw.print("Hello .");
81
}
82
}
83
84
static volatile boolean firstTime = true;
85
86
public void handle (HttpExchange req) {
87
try {
88
if(req.getRequestHeaders().get("Authorization") != null) {
89
authstring = req.getRequestHeaders().get("Authorization").get(0);
90
System.out.println(authstring);
91
}
92
if (firstTime) {
93
switch (count) {
94
case 0:
95
errorReply (req, "Basic realm=\"wallyworld\"");
96
break;
97
case 1:
98
/* client stores a username/pw for wallyworld
99
*/
100
save (authstring);
101
okReply (req);
102
break;
103
}
104
} else {
105
/* check the auth string is premptively set from last time */
106
String savedauth = retrieve();
107
if (savedauth.equals (authstring)) {
108
okReply (req);
109
} else {
110
System.out.println ("savedauth = " + savedauth);
111
System.out.println ("authstring = " + authstring);
112
errorReply (req, "Basic realm=\"wallyworld\"");
113
}
114
}
115
count ++;
116
} catch (IOException e) {
117
e.printStackTrace();
118
}
119
}
120
121
void save (String s) {
122
try {
123
FileOutputStream f = new FileOutputStream ("auth.save");
124
ObjectOutputStream os = new ObjectOutputStream (f);
125
os.writeObject (s);
126
} catch (IOException e) {
127
assert false;
128
}
129
}
130
131
String retrieve () {
132
String s = null;
133
try {
134
FileInputStream f = new FileInputStream ("auth.save");
135
ObjectInputStream is = new ObjectInputStream (f);
136
s = (String) is.readObject();
137
} catch (Exception e) {
138
assert false;
139
}
140
return s;
141
}
142
143
static void read (InputStream is) throws IOException {
144
int c;
145
System.out.println ("reading");
146
while ((c=is.read()) != -1) {
147
System.out.write (c);
148
}
149
System.out.println ("");
150
System.out.println ("finished reading");
151
}
152
153
static void client (String u) throws Exception {
154
URL url = new URL (u);
155
System.out.println ("client opening connection to: " + u);
156
URLConnection urlc = url.openConnection ();
157
try(InputStream is = urlc.getInputStream ()) {
158
read (is);
159
}
160
}
161
162
static HttpServer server;
163
164
public static void main (String[] args) throws Exception {
165
B4933582 b4933582 = new B4933582();
166
MyAuthenticator auth = new MyAuthenticator ();
167
Authenticator.setDefault (auth);
168
ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
169
InetAddress loopback = InetAddress.getLoopbackAddress();
170
CacheImpl cache;
171
try {
172
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
173
server.createContext("/", b4933582);
174
server.setExecutor(Executors.newSingleThreadExecutor());
175
server.start();
176
cache = new CacheImpl (server.getAddress().getPort());
177
AuthCacheValue.setAuthCache (cache);
178
String serverURL = URIBuilder.newBuilder()
179
.scheme("http")
180
.loopback()
181
.port(server.getAddress().getPort())
182
.path("/")
183
.build()
184
.toString();
185
client(serverURL + "d1/foo.html");
186
} finally {
187
if (server != null) {
188
server.stop(1);
189
}
190
}
191
192
int f = auth.getCount();
193
if (f != 1) {
194
except("Authenticator was called " + f + " times. Should be 1");
195
}
196
197
firstTime = false;
198
199
int retries = 0;
200
cache = new CacheImpl();
201
while (true) {
202
try {
203
server = HttpServer.create(new InetSocketAddress(loopback, cache.getPort()), 10);
204
server.createContext("/", b4933582);
205
server.setExecutor(Executors.newSingleThreadExecutor());
206
server.start();
207
break;
208
} catch (BindException e) {
209
if (retries++ < 5) {
210
Thread.sleep(200L);
211
System.out.println("BindException \"" + e.getMessage()
212
+ "\", retrying...");
213
continue;
214
} else {
215
throw e;
216
}
217
}
218
}
219
220
try {
221
AuthCacheValue.setAuthCache(cache);
222
String serverURL = URIBuilder.newBuilder()
223
.scheme("http")
224
.loopback()
225
.port(server.getAddress().getPort())
226
.path("/")
227
.build()
228
.toString();
229
client(serverURL + "d1/foo.html");
230
} finally {
231
if (server != null) {
232
server.stop(1);
233
}
234
}
235
236
f = auth.getCount();
237
if (f != 1) {
238
except("Authenticator was called " + f + " times. Should be 1");
239
}
240
}
241
242
public static void except (String s) {
243
server.stop(1);
244
throw new RuntimeException (s);
245
}
246
247
static class MyAuthenticator extends Authenticator {
248
MyAuthenticator () {
249
super ();
250
}
251
252
volatile int count = 0;
253
254
public PasswordAuthentication getPasswordAuthentication () {
255
PasswordAuthentication pw;
256
pw = new PasswordAuthentication ("user", "pass1".toCharArray());
257
count ++;
258
return pw;
259
}
260
261
public int getCount () {
262
return (count);
263
}
264
}
265
266
static class CacheImpl extends AuthCacheImpl {
267
HashMap<String,LinkedList<AuthCacheValue>> map;
268
int port; // need to store the port number the server is using
269
270
CacheImpl () throws IOException {
271
this (-1);
272
}
273
274
CacheImpl (int port) throws IOException {
275
super();
276
this.port = port;
277
File src = new File ("cache.ser");
278
if (src.exists()) {
279
try (ObjectInputStream is = new ObjectInputStream(
280
new FileInputStream(src))) {
281
map = (HashMap<String,LinkedList<AuthCacheValue>>)is
282
.readObject();
283
this.port = (Integer)is.readObject ();
284
System.out.println ("read port from file " + port);
285
} catch (ClassNotFoundException e) {
286
assert false;
287
}
288
System.out.println ("setMap from cache.ser");
289
} else {
290
map = new HashMap<>();
291
}
292
setMap (map);
293
}
294
295
int getPort () {
296
return port;
297
}
298
299
private void writeMap () {
300
File dst = new File("cache.ser");
301
try {
302
dst.delete();
303
if (!dst.createNewFile()) {
304
return;
305
}
306
} catch (IOException e) {
307
}
308
309
try (ObjectOutputStream os = new ObjectOutputStream(
310
new FileOutputStream(dst))) {
311
os.writeObject(map);
312
os.writeObject(port);
313
System.out.println("wrote port " + port);
314
} catch (IOException e) {
315
}
316
}
317
318
public void put (String pkey, AuthCacheValue value) {
319
System.out.println ("put: " + pkey + " " + value);
320
super.put (pkey, value);
321
writeMap();
322
}
323
324
public AuthCacheValue get (String pkey, String skey) {
325
System.out.println ("get: " + pkey + " " + skey);
326
AuthCacheValue i = super.get (pkey, skey);
327
System.out.println ("---> " + i);
328
return i;
329
}
330
331
public void remove (String pkey, AuthCacheValue value) {
332
System.out.println ("remove: " + pkey + " " + value);
333
super.remove (pkey, value);
334
writeMap();
335
}
336
}
337
}
338
339