File: Abort.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (416 lines) | stat: -rw-r--r-- 16,806 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @build DummyWebSocketServer
 * @run testng/othervm
 *      -Djdk.internal.httpclient.websocket.debug=true
 *       Abort
 */

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.ProtocolException;
import java.net.http.WebSocket;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static java.net.http.HttpClient.newHttpClient;
import static java.net.http.WebSocket.NORMAL_CLOSURE;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class Abort {

    private static final Class<NullPointerException> NPE = NullPointerException.class;
    private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
    private static final Class<IOException> IOE = IOException.class;

    private DummyWebSocketServer server;
    private WebSocket webSocket;

    @AfterTest
    public void cleanup() {
        server.close();
        webSocket.abort();
    }

    @Test
    public void onOpenThenAbort() throws Exception {
        int[] bytes = new int[]{
                0x88, 0x00, // opcode=close
        };
        server = Support.serverWithCannedData(bytes);
        server.open();
        // messages are available
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
                webSocket.abort();
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen as WebSocket was aborted
        assertEquals(inv, List.of(MockListener.Invocation.onOpen(webSocket)));
    }

    @Test
    public void onOpenThenOnTextThenAbort() throws Exception {
        int[] bytes = new int[]{
                0x81, 0x00, // opcode=text, fin=true
                0x88, 0x00, // opcode=close
        };
        server = Support.serverWithCannedData(bytes);
        server.open();
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
            }

            @Override
            protected CompletionStage<?> onText0(WebSocket webSocket,
                                                 CharSequence message,
                                                 boolean last) {
                webSocket.abort();
                return super.onText0(webSocket, message, last);
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen, onBinary as WebSocket was aborted
        List<MockListener.Invocation> expected = List.of(
                MockListener.Invocation.onOpen(webSocket),
                MockListener.Invocation.onText(webSocket, "", true));
        assertEquals(inv, expected);
    }

    @Test
    public void onOpenThenOnBinaryThenAbort() throws Exception {
        int[] bytes = new int[]{
                0x82, 0x00, // opcode=binary, fin=true
                0x88, 0x00, // opcode=close
        };
        server = Support.serverWithCannedData(bytes);
        server.open();
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
            }

            @Override
            protected CompletionStage<?> onBinary0(WebSocket webSocket,
                                                   ByteBuffer message,
                                                   boolean last) {
                webSocket.abort();
                return super.onBinary0(webSocket, message, last);
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen, onBinary as WebSocket was aborted
        List<MockListener.Invocation> expected = List.of(
                MockListener.Invocation.onOpen(webSocket),
                MockListener.Invocation.onBinary(webSocket, ByteBuffer.allocate(0), true));
        assertEquals(inv, expected);
    }

    @Test
    public void onOpenThenOnPingThenAbort() throws Exception {
        int[] bytes = {
                0x89, 0x00, // opcode=ping
                0x88, 0x00, // opcode=close
        };
        server = Support.serverWithCannedData(bytes);
        server.open();
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
            }

            @Override
            protected CompletionStage<?> onPing0(WebSocket webSocket,
                                                 ByteBuffer message) {
                webSocket.abort();
                return super.onPing0(webSocket, message);
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen, onPing as WebSocket was aborted
        List<MockListener.Invocation> expected = List.of(
                MockListener.Invocation.onOpen(webSocket),
                MockListener.Invocation.onPing(webSocket, ByteBuffer.allocate(0)));
        assertEquals(inv, expected);
    }

    @Test
    public void onOpenThenOnPongThenAbort() throws Exception {
        int[] bytes = {
                0x8a, 0x00, // opcode=pong
                0x88, 0x00, // opcode=close
        };
        server = Support.serverWithCannedData(bytes);
        server.open();
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
            }

            @Override
            protected CompletionStage<?> onPong0(WebSocket webSocket,
                                                 ByteBuffer message) {
                webSocket.abort();
                return super.onPong0(webSocket, message);
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen, onPong as WebSocket was aborted
        List<MockListener.Invocation> expected = List.of(
                MockListener.Invocation.onOpen(webSocket),
                MockListener.Invocation.onPong(webSocket, ByteBuffer.allocate(0)));
        assertEquals(inv, expected);
    }

    @Test
    public void onOpenThenOnCloseThenAbort() throws Exception {
        int[] bytes = {
                0x88, 0x00, // opcode=close
                0x8a, 0x00, // opcode=pong
        };
        server = Support.serverWithCannedData(bytes);
        server.open();
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
            }

            @Override
            protected CompletionStage<?> onClose0(WebSocket webSocket,
                                                  int statusCode,
                                                  String reason) {
                webSocket.abort();
                return super.onClose0(webSocket, statusCode, reason);
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen, onClose
        List<MockListener.Invocation> expected = List.of(
                MockListener.Invocation.onOpen(webSocket),
                MockListener.Invocation.onClose(webSocket, 1005, ""));
        assertEquals(inv, expected);
    }

    @Test
    public void onOpenThenOnErrorThenAbort() throws Exception {
        // A header of 128 bytes long Ping (which is a protocol error)
        int[] badPingHeader = new int[]{0x89, 0x7e, 0x00, 0x80};
        int[] closeMessage = new int[]{0x88, 0x00};
        int[] bytes = new int[badPingHeader.length + 128 + closeMessage.length];
        System.arraycopy(badPingHeader, 0, bytes, 0, badPingHeader.length);
        System.arraycopy(closeMessage, 0, bytes, badPingHeader.length + 128, closeMessage.length);
        server = Support.serverWithCannedData(bytes);
        server.open();
        MockListener listener = new MockListener() {
            @Override
            protected void onOpen0(WebSocket webSocket) {
                // unbounded request
                webSocket.request(Long.MAX_VALUE);
            }

            @Override
            protected void onError0(WebSocket webSocket, Throwable error) {
                webSocket.abort();
                super.onError0(webSocket, error);
            }
        };
        webSocket = newHttpClient().newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        TimeUnit.SECONDS.sleep(5);
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
        // no more invocations after onOpen, onError
        List<MockListener.Invocation> expected = List.of(
                MockListener.Invocation.onOpen(webSocket),
                MockListener.Invocation.onError(webSocket, ProtocolException.class));
        System.out.println("actual invocations:" + Arrays.toString(inv.toArray()));
        assertEquals(inv, expected);
    }

    @Test
    public void immediateAbort() throws Exception {
        CompletableFuture<Void> messageReceived = new CompletableFuture<>();
        WebSocket.Listener listener = new WebSocket.Listener() {

            @Override
            public void onOpen(WebSocket webSocket) {
                /* no initial request */
            }

            @Override
            public CompletionStage<?> onText(WebSocket webSocket,
                                             CharSequence message,
                                             boolean last) {
                messageReceived.complete(null);
                return null;
            }

            @Override
            public CompletionStage<?> onBinary(WebSocket webSocket,
                                               ByteBuffer message,
                                               boolean last) {
                messageReceived.complete(null);
                return null;
            }

            @Override
            public CompletionStage<?> onPing(WebSocket webSocket,
                                             ByteBuffer message) {
                messageReceived.complete(null);
                return null;
            }

            @Override
            public CompletionStage<?> onPong(WebSocket webSocket,
                                             ByteBuffer message) {
                messageReceived.complete(null);
                return null;
            }

            @Override
            public CompletionStage<?> onClose(WebSocket webSocket,
                                              int statusCode,
                                              String reason) {
                messageReceived.complete(null);
                return null;
            }
        };

        int[] bytes = new int[]{
                0x82, 0x00, // opcode=binary, fin=true
                0x88, 0x00, // opcode=close
        };
        server = Support.serverWithCannedData(bytes);
        server.open();

        WebSocket ws = newHttpClient()
                .newWebSocketBuilder()
                .buildAsync(server.getURI(), listener)
                .join();
        for (int i = 0; i < 3; i++) {
            System.out.printf("iteration #%s%n", i);
            // after the first abort() each consecutive one must be a no-op,
            // moreover, query methods should continue to return consistent
            // values
            for (int j = 0; j < 3; j++) {
                System.out.printf("abort #%s%n", j);
                ws.abort();
                assertTrue(ws.isInputClosed());
                assertTrue(ws.isOutputClosed());
                assertEquals(ws.getSubprotocol(), "");
            }
            // at this point valid requests MUST be a no-op:
            for (int j = 0; j < 3; j++) {
                System.out.printf("request #%s%n", j);
                ws.request(1);
                ws.request(2);
                ws.request(8);
                ws.request(Integer.MAX_VALUE);
                ws.request(Long.MAX_VALUE);
                // invalid requests MUST throw IAE:
                assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
                assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
                assertThrows(IAE, () -> ws.request(-1));
                assertThrows(IAE, () -> ws.request(0));
            }
        }
        // even though there is a bunch of messages readily available on the
        // wire we shouldn't have received any of them as we aborted before
        // the first request
        try {
            messageReceived.get(5, TimeUnit.SECONDS);
            fail();
        } catch (TimeoutException expected) {
            System.out.println("Finished waiting");
        }
        for (int i = 0; i < 3; i++) {
            System.out.printf("send #%s%n", i);
            Support.assertFails(IOE, ws.sendText("text!", false));
            Support.assertFails(IOE, ws.sendText("text!", true));
            Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
            Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
            Support.assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
            Support.assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
            Support.assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
            assertThrows(NPE, () -> ws.sendText(null, false));
            assertThrows(NPE, () -> ws.sendText(null, true));
            assertThrows(NPE, () -> ws.sendBinary(null, false));
            assertThrows(NPE, () -> ws.sendBinary(null, true));
            assertThrows(NPE, () -> ws.sendPing(null));
            assertThrows(NPE, () -> ws.sendPong(null));
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
        }
    }
}