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