author | chegar |
Tue, 19 Jun 2018 09:13:58 +0100 | |
branch | http-client-branch |
changeset 56771 | 73a6534bce94 |
parent 56744 | efbae6e5b3cd |
child 56795 | 03ece2518428 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
2 |
* Copyright (c) 2017, 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 javax.net.ServerSocketFactory; |
|
25 |
import javax.net.ssl.SSLContext; |
|
56771 | 26 |
import javax.net.ssl.SSLParameters; |
48083 | 27 |
import javax.net.ssl.SSLHandshakeException; |
28 |
import javax.net.ssl.SSLSocket; |
|
29 |
import java.io.DataInputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.UncheckedIOException; |
|
49765 | 32 |
import java.net.InetAddress; |
33 |
import java.net.InetSocketAddress; |
|
48083 | 34 |
import java.net.ServerSocket; |
35 |
import java.net.Socket; |
|
36 |
import java.net.URI; |
|
37 |
import java.util.List; |
|
38 |
import java.util.concurrent.CompletableFuture; |
|
39 |
import java.util.concurrent.CompletionException; |
|
49765 | 40 |
import java.net.http.HttpClient; |
41 |
import java.net.http.HttpClient.Version; |
|
42 |
import java.net.http.HttpResponse; |
|
43 |
import java.net.http.HttpRequest; |
|
48083 | 44 |
import static java.lang.System.out; |
49765 | 45 |
import static java.net.http.HttpResponse.BodyHandlers.discarding; |
48083 | 46 |
|
47 |
/** |
|
48 |
* @test |
|
56538
9bdcfc7d2b9c
http-client-branch: add a property to make it possible to limit the size of the HTTP/1.1 coonection pool
dfuchs
parents:
56451
diff
changeset
|
49 |
* @run main/othervm -Djdk.internal.httpclient.debug=true HandshakeFailureTest |
48083 | 50 |
* @summary Verify SSLHandshakeException is received when the handshake fails, |
51 |
* either because the server closes ( EOF ) the connection during handshaking |
|
52 |
* or no cipher suite ( or similar ) can be negotiated. |
|
53 |
*/ |
|
54 |
// To switch on debugging use: |
|
55 |
// @run main/othervm -Djdk.internal.httpclient.debug=true HandshakeFailureTest |
|
56 |
public class HandshakeFailureTest { |
|
57 |
||
58 |
// The number of iterations each testXXXClient performs. Can be increased |
|
59 |
// when running standalone testing. |
|
60 |
static final int TIMES = 10; |
|
61 |
||
62 |
public static void main(String[] args) throws Exception { |
|
63 |
HandshakeFailureTest test = new HandshakeFailureTest(); |
|
64 |
List<AbstractServer> servers = List.of( new PlainServer(), new SSLServer()); |
|
65 |
||
66 |
for (AbstractServer server : servers) { |
|
67 |
try (server) { |
|
68 |
out.format("%n%n------ Testing with server:%s ------%n", server); |
|
49765 | 69 |
URI uri = new URI("https://localhost:" + server.getPort() + "/"); |
48083 | 70 |
|
71 |
test.testSyncSameClient(uri, Version.HTTP_1_1); |
|
72 |
test.testSyncSameClient(uri, Version.HTTP_2); |
|
73 |
test.testSyncDiffClient(uri, Version.HTTP_1_1); |
|
74 |
test.testSyncDiffClient(uri, Version.HTTP_2); |
|
75 |
||
76 |
test.testAsyncSameClient(uri, Version.HTTP_1_1); |
|
77 |
test.testAsyncSameClient(uri, Version.HTTP_2); |
|
78 |
test.testAsyncDiffClient(uri, Version.HTTP_1_1); |
|
79 |
test.testAsyncDiffClient(uri, Version.HTTP_2); |
|
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
||
56771 | 84 |
static HttpClient getClient() { |
85 |
SSLParameters params = new SSLParameters(); |
|
86 |
params.setProtocols(new String[] {"TLSv1.2"}); |
|
87 |
return HttpClient.newBuilder() |
|
88 |
.sslParameters(params) |
|
89 |
.build(); |
|
90 |
} |
|
91 |
||
48083 | 92 |
void testSyncSameClient(URI uri, Version version) throws Exception { |
93 |
out.printf("%n--- testSyncSameClient %s ---%n", version); |
|
56771 | 94 |
HttpClient client = getClient(); |
48083 | 95 |
for (int i = 0; i < TIMES; i++) { |
96 |
out.printf("iteration %d%n", i); |
|
97 |
HttpRequest request = HttpRequest.newBuilder(uri) |
|
98 |
.version(version) |
|
99 |
.build(); |
|
100 |
try { |
|
49765 | 101 |
HttpResponse<Void> response = client.send(request, discarding()); |
48083 | 102 |
String msg = String.format("UNEXPECTED response=%s%n", response); |
103 |
throw new RuntimeException(msg); |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
104 |
} catch (IOException expected) { |
48083 | 105 |
out.printf("Client: caught expected exception: %s%n", expected); |
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
106 |
checkExceptionOrCause(SSLHandshakeException.class, expected); |
48083 | 107 |
} |
108 |
} |
|
109 |
} |
|
110 |
||
111 |
void testSyncDiffClient(URI uri, Version version) throws Exception { |
|
112 |
out.printf("%n--- testSyncDiffClient %s ---%n", version); |
|
113 |
for (int i = 0; i < TIMES; i++) { |
|
114 |
out.printf("iteration %d%n", i); |
|
115 |
// a new client each time |
|
56771 | 116 |
HttpClient client = getClient(); |
48083 | 117 |
HttpRequest request = HttpRequest.newBuilder(uri) |
118 |
.version(version) |
|
119 |
.build(); |
|
120 |
try { |
|
49765 | 121 |
HttpResponse<Void> response = client.send(request, discarding()); |
48083 | 122 |
String msg = String.format("UNEXPECTED response=%s%n", response); |
123 |
throw new RuntimeException(msg); |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
124 |
} catch (IOException expected) { |
48083 | 125 |
out.printf("Client: caught expected exception: %s%n", expected); |
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
126 |
checkExceptionOrCause(SSLHandshakeException.class, expected); |
48083 | 127 |
} |
128 |
} |
|
129 |
} |
|
130 |
||
131 |
void testAsyncSameClient(URI uri, Version version) throws Exception { |
|
132 |
out.printf("%n--- testAsyncSameClient %s ---%n", version); |
|
56771 | 133 |
HttpClient client = getClient(); |
48083 | 134 |
for (int i = 0; i < TIMES; i++) { |
135 |
out.printf("iteration %d%n", i); |
|
136 |
HttpRequest request = HttpRequest.newBuilder(uri) |
|
137 |
.version(version) |
|
138 |
.build(); |
|
139 |
CompletableFuture<HttpResponse<Void>> response = |
|
49765 | 140 |
client.sendAsync(request, discarding()); |
48083 | 141 |
try { |
142 |
response.join(); |
|
143 |
String msg = String.format("UNEXPECTED response=%s%n", response); |
|
144 |
throw new RuntimeException(msg); |
|
145 |
} catch (CompletionException ce) { |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
146 |
Throwable expected = ce.getCause(); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
147 |
out.printf("Client: caught expected exception: %s%n", expected); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
148 |
checkExceptionOrCause(SSLHandshakeException.class, expected); |
48083 | 149 |
} |
150 |
} |
|
151 |
} |
|
152 |
||
153 |
void testAsyncDiffClient(URI uri, Version version) throws Exception { |
|
154 |
out.printf("%n--- testAsyncDiffClient %s ---%n", version); |
|
155 |
for (int i = 0; i < TIMES; i++) { |
|
156 |
out.printf("iteration %d%n", i); |
|
157 |
// a new client each time |
|
56771 | 158 |
HttpClient client = getClient(); |
48083 | 159 |
HttpRequest request = HttpRequest.newBuilder(uri) |
160 |
.version(version) |
|
161 |
.build(); |
|
162 |
CompletableFuture<HttpResponse<Void>> response = |
|
49765 | 163 |
client.sendAsync(request, discarding()); |
48083 | 164 |
try { |
165 |
response.join(); |
|
166 |
String msg = String.format("UNEXPECTED response=%s%n", response); |
|
167 |
throw new RuntimeException(msg); |
|
168 |
} catch (CompletionException ce) { |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
169 |
ce.printStackTrace(out); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
170 |
Throwable expected = ce.getCause(); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
171 |
out.printf("Client: caught expected exception: %s%n", expected); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
172 |
checkExceptionOrCause(SSLHandshakeException.class, expected); |
48083 | 173 |
} |
174 |
} |
|
175 |
} |
|
176 |
||
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
177 |
static void checkExceptionOrCause(Class<? extends Throwable> clazz, Throwable t) { |
56744
efbae6e5b3cd
http-client-branch: review comment: possible NPE in tests
chegar
parents:
56682
diff
changeset
|
178 |
final Throwable original = t; |
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
179 |
do { |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
180 |
if (clazz.isInstance(t)) { |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
181 |
System.out.println("Found expected exception/cause: " + t); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
182 |
return; // found |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
183 |
} |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
184 |
} while ((t = t.getCause()) != null); |
56744
efbae6e5b3cd
http-client-branch: review comment: possible NPE in tests
chegar
parents:
56682
diff
changeset
|
185 |
original.printStackTrace(System.out); |
efbae6e5b3cd
http-client-branch: review comment: possible NPE in tests
chegar
parents:
56682
diff
changeset
|
186 |
throw new RuntimeException("Expected " + clazz + "in " + original); |
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
187 |
} |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56538
diff
changeset
|
188 |
|
48083 | 189 |
/** Common supertype for PlainServer and SSLServer. */ |
190 |
static abstract class AbstractServer extends Thread implements AutoCloseable { |
|
191 |
protected final ServerSocket ss; |
|
192 |
protected volatile boolean closed; |
|
193 |
||
194 |
AbstractServer(String name, ServerSocket ss) throws IOException { |
|
195 |
super(name); |
|
49765 | 196 |
ss.setReuseAddress(false); |
197 |
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); |
|
48083 | 198 |
this.ss = ss; |
199 |
this.start(); |
|
200 |
} |
|
201 |
||
202 |
int getPort() { return ss.getLocalPort(); } |
|
203 |
||
204 |
@Override |
|
205 |
public void close() { |
|
206 |
if (closed) |
|
207 |
return; |
|
208 |
closed = true; |
|
209 |
try { |
|
210 |
ss.close(); |
|
211 |
} catch (IOException e) { |
|
212 |
throw new UncheckedIOException("Unexpected", e); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
/** Emulates a server-side, using plain cleartext Sockets, that just closes |
|
218 |
* the connection, after a small variable delay. */ |
|
219 |
static class PlainServer extends AbstractServer { |
|
220 |
private volatile int count; |
|
221 |
||
222 |
PlainServer() throws IOException { |
|
49765 | 223 |
super("PlainServer", new ServerSocket()); |
48083 | 224 |
} |
225 |
||
226 |
@Override |
|
227 |
public void run() { |
|
228 |
while (!closed) { |
|
229 |
try (Socket s = ss.accept()) { |
|
230 |
count++; |
|
231 |
||
232 |
/* SSL record layer - contains the client hello |
|
233 |
struct { |
|
234 |
uint8 major, minor; |
|
235 |
} ProtocolVersion; |
|
236 |
||
237 |
enum { |
|
238 |
change_cipher_spec(20), alert(21), handshake(22), |
|
239 |
application_data(23), (255) |
|
240 |
} ContentType; |
|
241 |
||
242 |
struct { |
|
243 |
ContentType type; |
|
244 |
ProtocolVersion version; |
|
245 |
uint16 length; |
|
246 |
opaque fragment[SSLPlaintext.length]; |
|
247 |
} SSLPlaintext; */ |
|
248 |
DataInputStream din = new DataInputStream(s.getInputStream()); |
|
249 |
int contentType = din.read(); |
|
250 |
out.println("ContentType:" + contentType); |
|
251 |
int majorVersion = din.read(); |
|
252 |
out.println("Major:" + majorVersion); |
|
253 |
int minorVersion = din.read(); |
|
254 |
out.println("Minor:" + minorVersion); |
|
255 |
int length = din.readShort(); |
|
256 |
out.println("length:" + length); |
|
257 |
byte[] ba = new byte[length]; |
|
258 |
din.readFully(ba); |
|
259 |
||
260 |
// simulate various delays in response |
|
261 |
Thread.sleep(10 * (count % 10)); |
|
262 |
s.close(); // close without giving any reply |
|
263 |
} catch (IOException e) { |
|
264 |
if (!closed) |
|
265 |
out.println("Unexpected" + e); |
|
266 |
} catch (InterruptedException e) { |
|
267 |
throw new RuntimeException(e); |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
/** Emulates a server-side, using SSL Sockets, that will fail during |
|
274 |
* handshaking, as there are no cipher suites in common. */ |
|
275 |
static class SSLServer extends AbstractServer { |
|
276 |
static final SSLContext sslContext = createUntrustingContext(); |
|
277 |
static final ServerSocketFactory factory = sslContext.getServerSocketFactory(); |
|
278 |
||
279 |
static SSLContext createUntrustingContext() { |
|
280 |
try { |
|
281 |
SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); |
|
282 |
sslContext.init(null, null, null); |
|
283 |
return sslContext; |
|
284 |
} catch (Throwable t) { |
|
285 |
throw new AssertionError(t); |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
SSLServer() throws IOException { |
|
49765 | 290 |
super("SSLServer", factory.createServerSocket()); |
48083 | 291 |
} |
292 |
||
293 |
@Override |
|
294 |
public void run() { |
|
295 |
while (!closed) { |
|
296 |
try (SSLSocket s = (SSLSocket)ss.accept()) { |
|
297 |
s.getInputStream().read(); // will throw SHE here |
|
298 |
||
299 |
throw new AssertionError("Should not reach here"); |
|
300 |
} catch (SSLHandshakeException expected) { |
|
301 |
// Expected: SSLHandshakeException: no cipher suites in common |
|
302 |
out.printf("Server: caught expected exception: %s%n", expected); |
|
303 |
} catch (IOException e) { |
|
304 |
if (!closed) |
|
305 |
out.printf("UNEXPECTED %s", e); |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
} |
|
310 |
} |