Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/ALPNFailureTest.java
41149 views
1
/*
2
* Copyright (c) 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
* @summary This test will timeout if the ALPN CF is not completed
27
* when a 'Connection reset by peer' exception is raised
28
* during the handshake.
29
* @bug 8217094
30
* @modules java.net.http
31
* java.logging
32
* @build ALPNFailureTest
33
* @run main/othervm -Djdk.internal.httpclient.debug=true ALPNFailureTest HTTP_1_1
34
* @run main/othervm ALPNFailureTest HTTP_2
35
*/
36
import javax.net.ServerSocketFactory;
37
import javax.net.ssl.SSLContext;
38
import java.io.Closeable;
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.OutputStream;
42
import java.net.InetAddress;
43
import java.net.ProxySelector;
44
import java.net.ServerSocket;
45
import java.net.Socket;
46
import java.net.SocketTimeoutException;
47
import java.net.StandardSocketOptions;
48
import java.net.URI;
49
import java.net.http.HttpClient;
50
import java.net.http.HttpRequest;
51
import java.net.http.HttpResponse;
52
import java.net.http.HttpTimeoutException;
53
import java.util.List;
54
import java.util.concurrent.atomic.AtomicBoolean;
55
import java.util.concurrent.atomic.AtomicReference;
56
57
public class ALPNFailureTest {
58
59
60
public static void main(String[] args) throws Exception{
61
if (args == null || args.length == 0) {
62
args = new String[] {HttpClient.Version.HTTP_1_1.name()};
63
}
64
ServerSocket socket = ServerSocketFactory.getDefault()
65
.createServerSocket(0, 10, InetAddress.getLoopbackAddress());
66
67
test(socket, null, null, args);
68
}
69
70
public static void test(ServerSocket socket, SSLContext context,
71
ProxySelector ps, String... args)
72
throws Exception
73
{
74
System.out.println("Tests a race condition in SSLTube/SSLFlowDelegate");
75
System.out.println("This test will timeout if the ALPN CF is not completed" +
76
" when a 'Connection reset by peer' exception is raised" +
77
" during the handshake - see 8217094.");
78
79
URI uri = new URI("https", null,
80
socket.getInetAddress().getHostAddress(), socket.getLocalPort(),
81
"/ReadOnlyServer/https_1_1/", null, null);
82
HttpRequest request1 = HttpRequest.newBuilder(uri)
83
.GET().build();
84
HttpRequest request2 = HttpRequest.newBuilder(uri)
85
.POST(HttpRequest.BodyPublishers.ofString("foo")).build();
86
87
ReadOnlyServer server = new ReadOnlyServer(socket);
88
Thread serverThread = new Thread(server, "ServerThread");
89
serverThread.start();
90
try {
91
for (var arg : args) {
92
var version = HttpClient.Version.valueOf(arg);
93
HttpClient.Builder builder = HttpClient.newBuilder()
94
.version(version);
95
if (ps != null) builder.proxy(ps);
96
if (context != null) builder.sslContext(context);
97
98
HttpClient client = builder.build();
99
for (var request : List.of(request1, request2)) {
100
System.out.println("Server is " + socket.getLocalSocketAddress()
101
+ ", Version is " + version + ", Method is " + request.method()
102
+ (ps == null ? ", no proxy"
103
: (", Proxy is " + ps.select(request.uri()))));
104
try {
105
HttpResponse<String> resp =
106
client.send(request, HttpResponse.BodyHandlers.ofString());
107
throw new AssertionError(
108
"Client should not have received any response: " + resp);
109
} catch (HttpTimeoutException x) {
110
System.out.println("Unexpected " + x);
111
x.printStackTrace();
112
throw new AssertionError("Unexpected exception " + x, x);
113
} catch (Exception x) {
114
// We expect IOException("Connection reset by peer"), but
115
// any exception would do: we just don't want to linger
116
// forever.
117
System.err.println("Client got expected exception: " + x);
118
x.printStackTrace(System.out);
119
}
120
}
121
}
122
} finally {
123
server.close();
124
}
125
}
126
127
public static class ReadOnlyServer implements Runnable, Closeable {
128
final ServerSocket socket;
129
final AtomicReference<Throwable> errorRef = new AtomicReference<>();
130
final AtomicBoolean closing = new AtomicBoolean();
131
ReadOnlyServer(ServerSocket socket) {
132
this.socket = socket;
133
}
134
135
@Override
136
public void run() {
137
int count = 0;
138
int all = 0;
139
try {
140
System.out.println("Server starting");
141
while (!closing.get()) {
142
all += count;
143
count = 0;
144
try (Socket client = socket.accept()) {
145
client.setSoTimeout(1000);
146
client.setOption(StandardSocketOptions.SO_LINGER, 0);
147
InputStream is = client.getInputStream();
148
OutputStream os = client.getOutputStream();
149
boolean drain = true;
150
int timeouts = 0;
151
// now read some byte from the ClientHello
152
// and abruptly close the socket.
153
while (drain) {
154
try {
155
is.read();
156
count++;
157
if (count >= 50) {
158
drain = false;
159
}
160
} catch (SocketTimeoutException so) {
161
// make sure we read something
162
if (count > 0) timeouts++;
163
if (timeouts == 5) {
164
// presumably the client is
165
// waiting for us to answer...
166
// but we should not reach here.
167
drain = false;
168
}
169
}
170
}
171
System.out.println("Got " + count + " bytes");
172
}
173
}
174
} catch (Throwable t) {
175
if (!closing.get()) {
176
errorRef.set(t);
177
t.printStackTrace();
178
}
179
} finally {
180
System.out.println("Server existing after reading " + (all + count) + " bytes");
181
close();
182
}
183
184
}
185
186
@Override
187
public void close() {
188
if (closing.getAndSet(true))
189
return; // already closed
190
try {
191
socket.close();
192
} catch (IOException x) {
193
System.out.println("Exception while closing: " + x);
194
}
195
}
196
}
197
}
198
199