author | prappo |
Wed, 08 Jun 2016 15:19:58 +0100 | |
changeset 38864 | bf2b41533aed |
parent 37874 | 02589df0999a |
child 39133 | b5641ce64cf7 |
permissions | -rw-r--r-- |
37874 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 org.testng.annotations.Test; |
|
25 |
||
26 |
import java.io.IOException; |
|
27 |
import java.net.InetSocketAddress; |
|
28 |
import java.net.URI; |
|
29 |
import java.net.http.HttpClient; |
|
30 |
import java.net.http.WebSocket; |
|
31 |
import java.net.http.WebSocket.CloseCode; |
|
32 |
import java.nio.ByteBuffer; |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
33 |
import java.nio.CharBuffer; |
37874 | 34 |
import java.nio.channels.SocketChannel; |
35 |
import java.util.concurrent.CompletableFuture; |
|
36 |
import java.util.concurrent.TimeUnit; |
|
37 |
import java.util.function.Consumer; |
|
38 |
import java.util.stream.Stream; |
|
39 |
||
40 |
/* |
|
41 |
* @test |
|
42 |
* @bug 8087113 |
|
43 |
* @build TestKit |
|
44 |
* @run testng/othervm BasicWebSocketAPITest |
|
45 |
*/ |
|
46 |
public class BasicWebSocketAPITest { |
|
47 |
||
48 |
@Test |
|
49 |
public void webSocket() throws Exception { |
|
50 |
checkAndClose( |
|
51 |
(ws) -> |
|
52 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
53 |
"(?i).*\\bnegative\\b.*", |
|
54 |
() -> ws.request(-1)) |
|
55 |
); |
|
56 |
checkAndClose((ws) -> |
|
57 |
TestKit.assertNotThrows(() -> ws.request(0)) |
|
58 |
); |
|
59 |
checkAndClose((ws) -> |
|
60 |
TestKit.assertNotThrows(() -> ws.request(1)) |
|
61 |
); |
|
62 |
checkAndClose((ws) -> |
|
63 |
TestKit.assertNotThrows(() -> ws.request(Long.MAX_VALUE)) |
|
64 |
); |
|
65 |
checkAndClose((ws) -> |
|
66 |
TestKit.assertNotThrows(ws::isClosed) |
|
67 |
); |
|
68 |
checkAndClose((ws) -> |
|
69 |
TestKit.assertNotThrows(ws::getSubprotocol) |
|
70 |
); |
|
71 |
checkAndClose( |
|
72 |
(ws) -> { |
|
73 |
try { |
|
74 |
ws.abort(); |
|
75 |
} catch (IOException ignored) { } |
|
76 |
// No matter what happens during the first abort invocation, |
|
77 |
// other invocations must return normally |
|
78 |
TestKit.assertNotThrows(ws::abort); |
|
79 |
TestKit.assertNotThrows(ws::abort); |
|
80 |
} |
|
81 |
); |
|
82 |
checkAndClose( |
|
83 |
(ws) -> |
|
84 |
TestKit.assertThrows(NullPointerException.class, |
|
85 |
"message", |
|
86 |
() -> ws.sendBinary((byte[]) null, true)) |
|
87 |
); |
|
88 |
checkAndClose( |
|
89 |
(ws) -> |
|
90 |
TestKit.assertThrows(NullPointerException.class, |
|
91 |
"message", |
|
92 |
() -> ws.sendBinary((ByteBuffer) null, true)) |
|
93 |
); |
|
94 |
checkAndClose( |
|
95 |
(ws) -> |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
96 |
TestKit.assertThrows(IllegalArgumentException.class, |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
97 |
".*message.*", |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
98 |
() -> ws.sendPing(ByteBuffer.allocate(126))) |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
99 |
); |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
100 |
checkAndClose( |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
101 |
(ws) -> |
37874 | 102 |
TestKit.assertThrows(NullPointerException.class, |
103 |
"message", |
|
104 |
() -> ws.sendPing(null)) |
|
105 |
); |
|
106 |
checkAndClose( |
|
107 |
(ws) -> |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
108 |
TestKit.assertThrows(IllegalArgumentException.class, |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
109 |
".*message.*", |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
110 |
() -> ws.sendPong(ByteBuffer.allocate(126))) |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
111 |
); |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
112 |
checkAndClose( |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
113 |
(ws) -> |
37874 | 114 |
TestKit.assertThrows(NullPointerException.class, |
115 |
"message", |
|
116 |
() -> ws.sendPong(null)) |
|
117 |
); |
|
118 |
checkAndClose( |
|
119 |
(ws) -> |
|
120 |
TestKit.assertThrows(NullPointerException.class, |
|
121 |
"message", |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
122 |
() -> ws.sendText(null, true)) |
37874 | 123 |
); |
124 |
checkAndClose( |
|
125 |
(ws) -> |
|
126 |
TestKit.assertThrows(NullPointerException.class, |
|
127 |
"message", |
|
128 |
() -> ws.sendText((CharSequence) null)) |
|
129 |
); |
|
130 |
checkAndClose( |
|
131 |
(ws) -> |
|
132 |
TestKit.assertThrows(NullPointerException.class, |
|
133 |
"message", |
|
134 |
() -> ws.sendText((Stream<? extends CharSequence>) null)) |
|
135 |
); |
|
136 |
checkAndClose( |
|
137 |
(ws) -> |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
138 |
TestKit.assertThrows(IllegalArgumentException.class, |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
139 |
"(?i).*reason.*", |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
140 |
() -> ws.sendClose(CloseCode.NORMAL_CLOSURE, CharBuffer.allocate(124))) |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
141 |
); |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
142 |
checkAndClose( |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
143 |
(ws) -> |
37874 | 144 |
TestKit.assertThrows(NullPointerException.class, |
145 |
"code", |
|
146 |
() -> ws.sendClose(null, "")) |
|
147 |
); |
|
148 |
checkAndClose( |
|
149 |
(ws) -> |
|
150 |
TestKit.assertNotThrows( |
|
151 |
() -> ws.sendClose(CloseCode.NORMAL_CLOSURE, "")) |
|
152 |
); |
|
153 |
checkAndClose( |
|
154 |
(ws) -> |
|
155 |
TestKit.assertThrows(NullPointerException.class, |
|
156 |
"reason", |
|
157 |
() -> ws.sendClose(CloseCode.NORMAL_CLOSURE, null)) |
|
158 |
); |
|
159 |
checkAndClose( |
|
160 |
(ws) -> |
|
161 |
TestKit.assertThrows(NullPointerException.class, |
|
162 |
"code|reason", |
|
163 |
() -> ws.sendClose(null, null)) |
|
164 |
); |
|
165 |
} |
|
166 |
||
167 |
@Test |
|
168 |
public void builder() { |
|
169 |
URI ws = URI.create("ws://localhost:9001"); |
|
170 |
// FIXME: check all 24 cases: |
|
171 |
// {null, ws, wss, incorrect} x {null, HttpClient.getDefault(), custom} x {null, listener} |
|
172 |
// |
|
173 |
// if (any null) or (any incorrect) |
|
174 |
// NPE or IAE is thrown |
|
175 |
// else |
|
176 |
// builder is created |
|
177 |
TestKit.assertThrows(NullPointerException.class, |
|
178 |
"uri", |
|
179 |
() -> WebSocket.newBuilder(null, defaultListener()) |
|
180 |
); |
|
181 |
TestKit.assertThrows(NullPointerException.class, |
|
182 |
"listener", |
|
183 |
() -> WebSocket.newBuilder(ws, null) |
|
184 |
); |
|
185 |
URI uri = URI.create("ftp://localhost:9001"); |
|
186 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
187 |
"(?i).*\\buri\\b\\s+\\bscheme\\b.*", |
|
188 |
() -> WebSocket.newBuilder(uri, defaultListener()) |
|
189 |
); |
|
190 |
TestKit.assertNotThrows( |
|
191 |
() -> WebSocket.newBuilder(ws, defaultListener()) |
|
192 |
); |
|
193 |
URI uri1 = URI.create("wss://localhost:9001"); |
|
194 |
TestKit.assertNotThrows( |
|
195 |
() -> WebSocket.newBuilder(uri1, defaultListener()) |
|
196 |
); |
|
197 |
URI uri2 = URI.create("wss://localhost:9001#a"); |
|
198 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
199 |
"(?i).*\\bfragment\\b.*", |
|
200 |
() -> WebSocket.newBuilder(uri2, HttpClient.getDefault(), defaultListener()) |
|
201 |
); |
|
202 |
TestKit.assertThrows(NullPointerException.class, |
|
203 |
"uri", |
|
204 |
() -> WebSocket.newBuilder(null, HttpClient.getDefault(), defaultListener()) |
|
205 |
); |
|
206 |
TestKit.assertThrows(NullPointerException.class, |
|
207 |
"client", |
|
208 |
() -> WebSocket.newBuilder(ws, null, defaultListener()) |
|
209 |
); |
|
210 |
TestKit.assertThrows(NullPointerException.class, |
|
211 |
"listener", |
|
212 |
() -> WebSocket.newBuilder(ws, HttpClient.getDefault(), null) |
|
213 |
); |
|
214 |
// FIXME: check timeout works |
|
215 |
// (i.e. it directly influences the time WebSocket waits for connection + opening handshake) |
|
216 |
TestKit.assertNotThrows( |
|
217 |
() -> WebSocket.newBuilder(ws, defaultListener()).connectTimeout(1, TimeUnit.SECONDS) |
|
218 |
); |
|
219 |
WebSocket.Builder builder = WebSocket.newBuilder(ws, defaultListener()); |
|
220 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
221 |
"(?i).*\\bnegative\\b.*", |
|
222 |
() -> builder.connectTimeout(-1, TimeUnit.SECONDS) |
|
223 |
); |
|
224 |
WebSocket.Builder builder1 = WebSocket.newBuilder(ws, defaultListener()); |
|
225 |
TestKit.assertThrows(NullPointerException.class, |
|
226 |
"unit", |
|
227 |
() -> builder1.connectTimeout(1, null) |
|
228 |
); |
|
229 |
// FIXME: check these headers are actually received by the server |
|
230 |
TestKit.assertNotThrows( |
|
231 |
() -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b") |
|
232 |
); |
|
233 |
TestKit.assertNotThrows( |
|
234 |
() -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b").header("a", "b") |
|
235 |
); |
|
236 |
// FIXME: check all 18 cases: |
|
237 |
// {null, websocket(7), custom} x {null, custom} |
|
238 |
WebSocket.Builder builder2 = WebSocket.newBuilder(ws, defaultListener()); |
|
239 |
TestKit.assertThrows(NullPointerException.class, |
|
240 |
"name", |
|
241 |
() -> builder2.header(null, "b") |
|
242 |
); |
|
243 |
WebSocket.Builder builder3 = WebSocket.newBuilder(ws, defaultListener()); |
|
244 |
TestKit.assertThrows(NullPointerException.class, |
|
245 |
"value", |
|
246 |
() -> builder3.header("a", null) |
|
247 |
); |
|
248 |
WebSocket.Builder builder4 = WebSocket.newBuilder(ws, defaultListener()); |
|
249 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
250 |
() -> builder4.header("Sec-WebSocket-Accept", "") |
|
251 |
); |
|
252 |
WebSocket.Builder builder5 = WebSocket.newBuilder(ws, defaultListener()); |
|
253 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
254 |
() -> builder5.header("Sec-WebSocket-Extensions", "") |
|
255 |
); |
|
256 |
WebSocket.Builder builder6 = WebSocket.newBuilder(ws, defaultListener()); |
|
257 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
258 |
() -> builder6.header("Sec-WebSocket-Key", "") |
|
259 |
); |
|
260 |
WebSocket.Builder builder7 = WebSocket.newBuilder(ws, defaultListener()); |
|
261 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
262 |
() -> builder7.header("Sec-WebSocket-Protocol", "") |
|
263 |
); |
|
264 |
WebSocket.Builder builder8 = WebSocket.newBuilder(ws, defaultListener()); |
|
265 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
266 |
() -> builder8.header("Sec-WebSocket-Version", "") |
|
267 |
); |
|
268 |
WebSocket.Builder builder9 = WebSocket.newBuilder(ws, defaultListener()); |
|
269 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
270 |
() -> builder9.header("Connection", "") |
|
271 |
); |
|
272 |
WebSocket.Builder builder10 = WebSocket.newBuilder(ws, defaultListener()); |
|
273 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
274 |
() -> builder10.header("Upgrade", "") |
|
275 |
); |
|
276 |
// FIXME: check 3 cases (1 arg): |
|
277 |
// {null, incorrect, custom} |
|
278 |
// FIXME: check 12 cases (2 args): |
|
279 |
// {null, incorrect, custom} x {(String) null, (String[]) null, incorrect, custom} |
|
280 |
// FIXME: check 27 cases (3 args) (the interesting part in null inside var-arg): |
|
281 |
// {null, incorrect, custom}^3 |
|
282 |
// FIXME: check the server receives them in the order listed |
|
283 |
TestKit.assertThrows(NullPointerException.class, |
|
284 |
"mostPreferred", |
|
285 |
() -> WebSocket.newBuilder(ws, defaultListener()).subprotocols(null) |
|
286 |
); |
|
287 |
TestKit.assertThrows(NullPointerException.class, |
|
288 |
"lesserPreferred", |
|
289 |
() -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null) |
|
290 |
); |
|
291 |
TestKit.assertThrows(NullPointerException.class, |
|
292 |
"lesserPreferred\\[0\\]", |
|
293 |
() -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null, "b") |
|
294 |
); |
|
295 |
TestKit.assertThrows(NullPointerException.class, |
|
296 |
"lesserPreferred\\[1\\]", |
|
297 |
() -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", null) |
|
298 |
); |
|
299 |
TestKit.assertNotThrows( |
|
300 |
() -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a") |
|
301 |
); |
|
302 |
TestKit.assertNotThrows( |
|
303 |
() -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", "c") |
|
304 |
); |
|
305 |
WebSocket.Builder builder11 = WebSocket.newBuilder(ws, defaultListener()); |
|
306 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
307 |
() -> builder11.subprotocols("") |
|
308 |
); |
|
309 |
WebSocket.Builder builder12 = WebSocket.newBuilder(ws, defaultListener()); |
|
310 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
311 |
() -> builder12.subprotocols("a", "a") |
|
312 |
); |
|
313 |
WebSocket.Builder builder13 = WebSocket.newBuilder(ws, defaultListener()); |
|
314 |
TestKit.assertThrows(IllegalArgumentException.class, |
|
315 |
() -> builder13.subprotocols("a" + ((char) 0x7f)) |
|
316 |
); |
|
317 |
} |
|
318 |
||
319 |
private static WebSocket.Listener defaultListener() { |
|
320 |
return new WebSocket.Listener() { }; |
|
321 |
} |
|
322 |
||
323 |
// |
|
324 |
// Automatically closes everything after the check has been performed |
|
325 |
// |
|
326 |
private static void checkAndClose(Consumer<? super WebSocket> c) { |
|
327 |
HandshakePhase HandshakePhase |
|
328 |
= new HandshakePhase(new InetSocketAddress("127.0.0.1", 0)); |
|
329 |
URI serverURI = HandshakePhase.getURI(); |
|
330 |
CompletableFuture<SocketChannel> cfc = HandshakePhase.afterHandshake(); |
|
331 |
WebSocket.Builder b = WebSocket.newBuilder(serverURI, defaultListener()); |
|
332 |
CompletableFuture<WebSocket> cfw = b.buildAsync(); |
|
333 |
||
334 |
try { |
|
335 |
WebSocket ws; |
|
336 |
try { |
|
337 |
ws = cfw.get(); |
|
338 |
} catch (Exception e) { |
|
339 |
throw new RuntimeException(e); |
|
340 |
} |
|
341 |
c.accept(ws); |
|
342 |
} finally { |
|
343 |
try { |
|
344 |
SocketChannel now = cfc.getNow(null); |
|
345 |
if (now != null) { |
|
346 |
now.close(); |
|
347 |
} |
|
348 |
} catch (Throwable ignored) { } |
|
349 |
} |
|
350 |
} |
|
351 |
} |