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