|
1 /* |
|
2 * Copyright (c) 2018, 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 Test to ensure the HTTP client throws an appropriate SSL exception |
|
27 * when SSL context is not valid. |
|
28 * @library /lib/testlibrary |
|
29 * @build jdk.testlibrary.SimpleSSLContext |
|
30 * @run testng/othervm -Djdk.internal.httpclient.debug=true InvalidSSLContextTest |
|
31 */ |
|
32 |
|
33 import java.io.IOException; |
|
34 import java.io.UncheckedIOException; |
|
35 import java.net.URI; |
|
36 import java.util.concurrent.CompletableFuture; |
|
37 import java.util.concurrent.CompletionException; |
|
38 import javax.net.ssl.SSLContext; |
|
39 import javax.net.ssl.SSLException; |
|
40 import javax.net.ssl.SSLHandshakeException; |
|
41 import javax.net.ssl.SSLServerSocket; |
|
42 import javax.net.ssl.SSLSocket; |
|
43 import jdk.incubator.http.HttpClient; |
|
44 import jdk.incubator.http.HttpClient.Version; |
|
45 import jdk.incubator.http.HttpRequest; |
|
46 import jdk.incubator.http.HttpResponse; |
|
47 import jdk.incubator.http.HttpResponse.BodyHandler; |
|
48 import jdk.testlibrary.SimpleSSLContext; |
|
49 import org.testng.Assert; |
|
50 import org.testng.annotations.AfterTest; |
|
51 import org.testng.annotations.BeforeTest; |
|
52 import org.testng.annotations.DataProvider; |
|
53 import org.testng.annotations.Test; |
|
54 import static jdk.incubator.http.HttpClient.Version.HTTP_1_1; |
|
55 import static jdk.incubator.http.HttpClient.Version.HTTP_2; |
|
56 |
|
57 |
|
58 public class InvalidSSLContextTest { |
|
59 |
|
60 SSLContext sslContext; |
|
61 volatile SSLServerSocket sslServerSocket; |
|
62 volatile String uri; |
|
63 |
|
64 @DataProvider(name = "versions") |
|
65 public Object[][] versions() { |
|
66 return new Object[][]{ |
|
67 { HTTP_1_1 }, |
|
68 { HTTP_2 } |
|
69 }; |
|
70 } |
|
71 |
|
72 @Test(dataProvider = "versions") |
|
73 public void testSync(Version version) throws Exception { |
|
74 // client-side uses a different context to that of the server-side |
|
75 HttpClient client = HttpClient.newBuilder() |
|
76 .sslContext(SSLContext.getDefault()) |
|
77 .build(); |
|
78 |
|
79 HttpRequest request = HttpRequest.newBuilder(URI.create(uri)) |
|
80 .version(version) |
|
81 .build(); |
|
82 |
|
83 try { |
|
84 HttpResponse<?> response = client.send(request, BodyHandler.discard("")); |
|
85 Assert.fail("UNEXPECTED response" + response); |
|
86 } catch (SSLException sslex) { |
|
87 System.out.println("Caught expected: " + sslex); |
|
88 } |
|
89 } |
|
90 |
|
91 @Test(dataProvider = "versions") |
|
92 public void testAsync(Version version) throws Exception { |
|
93 // client-side uses a different context to that of the server-side |
|
94 HttpClient client = HttpClient.newBuilder() |
|
95 .sslContext(SSLContext.getDefault()) |
|
96 .build(); |
|
97 |
|
98 HttpRequest request = HttpRequest.newBuilder(URI.create(uri)) |
|
99 .version(version) |
|
100 .build(); |
|
101 |
|
102 assertExceptionally(SSLException.class, |
|
103 client.sendAsync(request, BodyHandler.discard(""))); |
|
104 } |
|
105 |
|
106 static void assertExceptionally(Class<? extends Throwable> clazz, |
|
107 CompletableFuture<?> stage) { |
|
108 stage.handle((result, error) -> { |
|
109 if (result != null) { |
|
110 Assert.fail("UNEXPECTED result: " + result); |
|
111 return null; |
|
112 } |
|
113 if (error instanceof CompletionException) { |
|
114 Throwable cause = error.getCause(); |
|
115 if (cause == null) { |
|
116 Assert.fail("Unexpected null cause: " + error); |
|
117 } |
|
118 assertException(clazz, cause); |
|
119 } else { |
|
120 assertException(clazz, error); |
|
121 } |
|
122 return null; |
|
123 }).join(); |
|
124 } |
|
125 |
|
126 static void assertException(Class<? extends Throwable> clazz, Throwable t) { |
|
127 if (t == null) { |
|
128 Assert.fail("Expected " + clazz + ", caught nothing"); |
|
129 } |
|
130 if (!clazz.isInstance(t)) { |
|
131 Assert.fail("Expected " + clazz + ", caught " + t); |
|
132 } |
|
133 } |
|
134 |
|
135 @BeforeTest |
|
136 public void setup() throws Exception { |
|
137 sslContext = new SimpleSSLContext().get(); |
|
138 if (sslContext == null) |
|
139 throw new AssertionError("Unexpected null sslContext"); |
|
140 |
|
141 // server-side uses a different context to that of the client-side |
|
142 sslServerSocket = (SSLServerSocket)sslContext |
|
143 .getServerSocketFactory() |
|
144 .createServerSocket(0); |
|
145 uri = "https://localhost:" + sslServerSocket.getLocalPort() + "/"; |
|
146 |
|
147 Thread t = new Thread("SSL-Server-Side") { |
|
148 @Override |
|
149 public void run() { |
|
150 while (true) { |
|
151 try { |
|
152 SSLSocket s = (SSLSocket) sslServerSocket.accept(); |
|
153 System.out.println("SERVER: accepted: " + s); |
|
154 // artificially slow down the handshake reply to mimic |
|
155 // a slow(ish) network, and hopefully delay the |
|
156 // SequentialScheduler on in the client. |
|
157 Thread.sleep(500); |
|
158 s.startHandshake(); |
|
159 s.close(); |
|
160 Assert.fail("SERVER: UNEXPECTED "); |
|
161 } catch (SSLHandshakeException he) { |
|
162 System.out.println("SERVER: caught expected " + he); |
|
163 } catch (IOException e) { |
|
164 System.out.println("SERVER: caught: " + e); |
|
165 if (!sslServerSocket.isClosed()) { |
|
166 throw new UncheckedIOException(e); |
|
167 } |
|
168 break; |
|
169 } catch (InterruptedException ie) { |
|
170 throw new RuntimeException(ie); |
|
171 } |
|
172 } |
|
173 } |
|
174 }; |
|
175 t.start(); |
|
176 } |
|
177 |
|
178 @AfterTest |
|
179 public void teardown() throws Exception { |
|
180 sslServerSocket.close(); |
|
181 } |
|
182 } |