|
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 * @build DummyWebSocketServer |
|
27 * @run testng/othervm |
|
28 * -Djdk.internal.httpclient.websocket.debug=true |
|
29 * ImmediateAbort |
|
30 */ |
|
31 |
|
32 import java.io.IOException; |
|
33 import java.net.http.WebSocket; |
|
34 import java.nio.ByteBuffer; |
|
35 import java.nio.channels.SocketChannel; |
|
36 import java.util.Arrays; |
|
37 import java.util.concurrent.CompletableFuture; |
|
38 import java.util.concurrent.CompletionStage; |
|
39 import java.util.concurrent.TimeUnit; |
|
40 import java.util.concurrent.TimeoutException; |
|
41 import org.testng.annotations.Test; |
|
42 import static java.net.http.HttpClient.newHttpClient; |
|
43 import static java.net.http.WebSocket.NORMAL_CLOSURE; |
|
44 import static org.testng.Assert.assertEquals; |
|
45 import static org.testng.Assert.assertThrows; |
|
46 import static org.testng.Assert.assertTrue; |
|
47 import static org.testng.Assert.fail; |
|
48 |
|
49 public class ImmediateAbort { |
|
50 |
|
51 private static final Class<NullPointerException> NPE = NullPointerException.class; |
|
52 private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class; |
|
53 private static final Class<IOException> IOE = IOException.class; |
|
54 |
|
55 /* |
|
56 * Examines WebSocket behaviour after a call to abort() |
|
57 */ |
|
58 @Test |
|
59 public void immediateAbort() throws Exception { |
|
60 try (DummyWebSocketServer server = serverWithCannedData(0x81, 0x00, 0x88, 0x00)) { |
|
61 server.open(); |
|
62 CompletableFuture<Void> messageReceived = new CompletableFuture<>(); |
|
63 WebSocket.Listener listener = new WebSocket.Listener() { |
|
64 |
|
65 @Override |
|
66 public void onOpen(WebSocket webSocket) { |
|
67 /* no initial request */ |
|
68 } |
|
69 |
|
70 @Override |
|
71 public CompletionStage<?> onText(WebSocket webSocket, |
|
72 CharSequence message, |
|
73 WebSocket.MessagePart part) { |
|
74 messageReceived.complete(null); |
|
75 return null; |
|
76 } |
|
77 |
|
78 @Override |
|
79 public CompletionStage<?> onBinary(WebSocket webSocket, |
|
80 ByteBuffer message, |
|
81 WebSocket.MessagePart part) { |
|
82 messageReceived.complete(null); |
|
83 return null; |
|
84 } |
|
85 |
|
86 @Override |
|
87 public CompletionStage<?> onPing(WebSocket webSocket, |
|
88 ByteBuffer message) { |
|
89 messageReceived.complete(null); |
|
90 return null; |
|
91 } |
|
92 |
|
93 @Override |
|
94 public CompletionStage<?> onPong(WebSocket webSocket, |
|
95 ByteBuffer message) { |
|
96 messageReceived.complete(null); |
|
97 return null; |
|
98 } |
|
99 |
|
100 @Override |
|
101 public CompletionStage<?> onClose(WebSocket webSocket, |
|
102 int statusCode, |
|
103 String reason) { |
|
104 messageReceived.complete(null); |
|
105 return null; |
|
106 } |
|
107 }; |
|
108 |
|
109 WebSocket ws = newHttpClient() |
|
110 .newWebSocketBuilder() |
|
111 .buildAsync(server.getURI(), listener) |
|
112 .join(); |
|
113 for (int i = 0; i < 3; i++) { |
|
114 System.out.printf("iteration #%s%n", i); |
|
115 // after the first abort() each consecutive one must be a no-op, |
|
116 // moreover, query methods should continue to return consistent, |
|
117 // permanent values |
|
118 for (int j = 0; j < 3; j++) { |
|
119 System.out.printf("abort #%s%n", j); |
|
120 ws.abort(); |
|
121 assertTrue(ws.isInputClosed()); |
|
122 assertTrue(ws.isOutputClosed()); |
|
123 assertEquals(ws.getSubprotocol(), ""); |
|
124 } |
|
125 // at this point valid requests MUST be a no-op: |
|
126 for (int j = 0; j < 3; j++) { |
|
127 System.out.printf("request #%s%n", j); |
|
128 ws.request(1); |
|
129 ws.request(2); |
|
130 ws.request(8); |
|
131 ws.request(Integer.MAX_VALUE); |
|
132 ws.request(Long.MAX_VALUE); |
|
133 // invalid requests MUST throw IAE: |
|
134 assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE)); |
|
135 assertThrows(IAE, () -> ws.request(Long.MIN_VALUE)); |
|
136 assertThrows(IAE, () -> ws.request(-1)); |
|
137 assertThrows(IAE, () -> ws.request(0)); |
|
138 } |
|
139 } |
|
140 // even though there is a bunch of messages readily available on the |
|
141 // wire we shouldn't have received any of them as we aborted before |
|
142 // the first request |
|
143 try { |
|
144 messageReceived.get(10, TimeUnit.SECONDS); |
|
145 fail(); |
|
146 } catch (TimeoutException expected) { |
|
147 System.out.println("Finished waiting"); |
|
148 } |
|
149 for (int i = 0; i < 3; i++) { |
|
150 System.out.printf("send #%s%n", i); |
|
151 assertFails(IOE, ws.sendText("text!", false)); |
|
152 assertFails(IOE, ws.sendText("text!", true)); |
|
153 assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false)); |
|
154 assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true)); |
|
155 assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16))); |
|
156 assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16))); |
|
157 assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason")); |
|
158 assertThrows(NPE, () -> ws.sendText(null, false)); |
|
159 assertThrows(NPE, () -> ws.sendText(null, true)); |
|
160 assertThrows(NPE, () -> ws.sendBinary(null, false)); |
|
161 assertThrows(NPE, () -> ws.sendBinary(null, true)); |
|
162 assertThrows(NPE, () -> ws.sendPing(null)); |
|
163 assertThrows(NPE, () -> ws.sendPong(null)); |
|
164 assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null)); |
|
165 } |
|
166 } |
|
167 } |
|
168 |
|
169 private static void assertFails(Class<? extends Throwable> clazz, |
|
170 CompletionStage<?> stage) { |
|
171 Support.assertCompletesExceptionally(clazz, stage); |
|
172 } |
|
173 |
|
174 private static DummyWebSocketServer serverWithCannedData(int... data) { |
|
175 byte[] copy = new byte[data.length]; |
|
176 for (int i = 0; i < data.length; i++) { |
|
177 copy[i] = (byte) data[i]; |
|
178 } |
|
179 return serverWithCannedData(copy); |
|
180 } |
|
181 |
|
182 private static DummyWebSocketServer serverWithCannedData(byte... data) { |
|
183 byte[] copy = Arrays.copyOf(data, data.length); |
|
184 return new DummyWebSocketServer() { |
|
185 @Override |
|
186 protected void serve(SocketChannel channel) throws IOException { |
|
187 ByteBuffer closeMessage = ByteBuffer.wrap(copy); |
|
188 channel.write(closeMessage); |
|
189 super.serve(channel); |
|
190 } |
|
191 }; |
|
192 } |
|
193 } |