Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/Deadlock.java
41152 views
1
/*
2
* Copyright (c) 2010, 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 6648001
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main/othervm/timeout=20 -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock
30
* @run main/othervm/timeout=20 -Djava.net.preferIPv6Addresses=true
31
* -ea:sun.net.www.protocol.http.AuthenticationInfo -Dhttp.auth.serializeRequests=true Deadlock
32
* @summary cancelling HTTP authentication causes deadlock
33
*/
34
35
import java.util.concurrent.Executors;
36
import java.util.concurrent.ExecutorService;
37
import java.io.InputStream;
38
import java.io.IOException;
39
import java.net.HttpURLConnection;
40
import java.net.InetAddress;
41
import java.net.InetSocketAddress;
42
import java.net.PasswordAuthentication;
43
import java.net.Proxy;
44
import java.net.URL;
45
import com.sun.net.httpserver.BasicAuthenticator;
46
import com.sun.net.httpserver.Headers;
47
import com.sun.net.httpserver.HttpContext;
48
import com.sun.net.httpserver.HttpExchange;
49
import com.sun.net.httpserver.HttpHandler;
50
import com.sun.net.httpserver.HttpPrincipal;
51
import com.sun.net.httpserver.HttpServer;
52
import jdk.test.lib.net.URIBuilder;
53
54
public class Deadlock {
55
56
public static void main (String[] args) throws Exception {
57
Handler handler = new Handler();
58
InetAddress loopback = InetAddress.getLoopbackAddress();
59
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
60
HttpServer server = HttpServer.create(addr, 0);
61
HttpContext ctx = server.createContext("/test", handler);
62
BasicAuthenticator a = new BasicAuthenticator("[email protected]") {
63
@Override
64
public boolean checkCredentials (String username, String pw) {
65
return "fred".equals(username) && pw.charAt(0) == 'x';
66
}
67
};
68
69
ctx.setAuthenticator(a);
70
ExecutorService executor = Executors.newCachedThreadPool();
71
server.setExecutor(executor);
72
server.start ();
73
java.net.Authenticator.setDefault(new MyAuthenticator());
74
75
System.out.print("Deadlock: " );
76
for (int i=0; i<2; i++) {
77
Runner t = new Runner(server, i);
78
t.start();
79
t.join();
80
}
81
server.stop(2);
82
executor.shutdown();
83
if (error) {
84
throw new RuntimeException("test failed error");
85
}
86
87
if (count != 2) {
88
throw new RuntimeException("test failed count = " + count);
89
}
90
System.out.println("OK");
91
92
}
93
94
static class Runner extends Thread {
95
HttpServer server;
96
int i;
97
Runner(HttpServer s, int i) {
98
server = s;
99
this.i = i;
100
}
101
102
@Override
103
public void run() {
104
URL url;
105
HttpURLConnection urlc;
106
try {
107
url = URIBuilder.newBuilder()
108
.scheme("http")
109
.loopback()
110
.port(server.getAddress().getPort())
111
.path("/test/foo.html")
112
.toURLUnchecked();
113
urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
114
} catch (IOException e) {
115
error = true;
116
return;
117
}
118
InputStream is = null;
119
try {
120
is = urlc.getInputStream();
121
while (is.read()!= -1) {}
122
} catch (IOException e) {
123
if (i == 1) error = true;
124
} finally {
125
if (is != null) try { is.close(); } catch (IOException e) {}
126
}
127
}
128
}
129
130
public static boolean error = false;
131
public static int count = 0;
132
133
static class MyAuthenticator extends java.net.Authenticator {
134
@Override
135
public PasswordAuthentication getPasswordAuthentication() {
136
PasswordAuthentication pw;
137
if (!getRequestingPrompt().equals("[email protected]")) {
138
Deadlock.error = true;
139
}
140
if (count == 0) {
141
pw = null;
142
} else {
143
pw = new PasswordAuthentication("fred", "xyz".toCharArray());
144
}
145
count++;
146
return pw;
147
}
148
}
149
150
static class Handler implements HttpHandler {
151
int invocation = 1;
152
153
@Override
154
public void handle (HttpExchange t)
155
throws IOException
156
{
157
InputStream is = t.getRequestBody();
158
Headers map = t.getRequestHeaders();
159
Headers rmap = t.getResponseHeaders();
160
while (is.read() != -1);
161
is.close();
162
t.sendResponseHeaders(200, -1);
163
HttpPrincipal p = t.getPrincipal();
164
if (!p.getUsername().equals("fred")) {
165
error = true;
166
}
167
if (!p.getRealm().equals("[email protected]")) {
168
error = true;
169
}
170
t.close();
171
}
172
}
173
}
174
175