OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
5 | 9 |
6 // TODO(7157): Remove this test once the bug is fixed. | 10 // TODO(7157): Remove this test once the bug is fixed. |
7 // This is a copy of web_socket_test.dart with the secure connection | 11 // This is a copy of web_socket_test.dart with the secure connection |
8 // tests disabled, so it does not crash on Windows. | 12 // tests disabled, so it does not crash on Windows. |
9 import "dart:async"; | |
10 import "dart:io"; | 13 import "dart:io"; |
11 import "dart:isolate"; | 14 import "dart:isolate"; |
12 import "dart:scalarlist"; | 15 import "dart:scalarlist"; |
13 import "dart:uri"; | 16 |
14 | 17 void testRequestResponseClientCloses( |
15 const SERVER_ADDRESS = "127.0.0.1"; | 18 int totalConnections, int closeStatus, String closeReason) { |
16 const HOST_NAME = "localhost"; | 19 HttpServer.bind().then((server) { |
17 | 20 |
18 | 21 server.transform(new WebSocketTransformer()).listen((webSocket) { |
19 class WebSocketInfo { | 22 webSocket.listen((event) { |
20 int messageCount = 0; | 23 if (event is MessageEvent) { |
21 } | 24 webSocket.send(event.data); |
22 | 25 } else if (event is CloseEvent) { |
23 | 26 Expect.equals(closeStatus == null |
24 /** | 27 ? WebSocketStatus.NO_STATUS_RECEIVED |
25 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. | 28 : closeStatus, event.code); |
26 */ | 29 Expect.equals(closeReason == null ? "" : closeReason, event.reason); |
27 class SecurityConfiguration { | 30 } |
28 final bool secure; | 31 }); |
29 HttpClient client; | 32 }); |
30 | |
31 SecurityConfiguration({bool this.secure}) : client = new HttpClient(); | |
32 | |
33 HttpServer createServer({int backlog}) { | |
34 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
35 server.listen(SERVER_ADDRESS, | |
36 0, | |
37 backlog: backlog, | |
38 certificate_name: "CN=$HOST_NAME"); | |
39 return server; | |
40 } | |
41 | |
42 WebSocketClientConnection createClient(int port, | |
43 {bool followRedirects, | |
44 String method: "GET"}) { | |
45 HttpClientConnection conn = client.openUrl(method, Uri.parse( | |
46 '${secure ? "https" : "http"}://$HOST_NAME:$port/')); | |
47 if (followRedirects != null) { | |
48 conn.followRedirects = followRedirects; | |
49 } | |
50 return new WebSocketClientConnection(conn); | |
51 } | |
52 | |
53 void testRequestResponseClientCloses( | |
54 int totalConnections, int closeStatus, String closeReason) { | |
55 HttpServer server = createServer(backlog: totalConnections); | |
56 HttpClient client = new HttpClient(); | |
57 | |
58 // Make a web socket handler and set it as the HTTP server default handler. | |
59 WebSocketHandler wsHandler = new WebSocketHandler(); | |
60 wsHandler.onOpen = (WebSocketConnection conn) { | |
61 var count = 0; | |
62 conn.onMessage = (Object message) => conn.send(message); | |
63 conn.onClosed = (status, reason) { | |
64 Expect.equals(closeStatus == null | |
65 ? WebSocketStatus.NO_STATUS_RECEIVED | |
66 : closeStatus, status); | |
67 Expect.equals(closeReason == null ? "" : closeReason, reason); | |
68 }; | |
69 }; | |
70 server.defaultRequestHandler = wsHandler.onRequest; | |
71 | 33 |
72 int closeCount = 0; | 34 int closeCount = 0; |
73 String messageText = "Hello, world!"; | 35 String messageText = "Hello, world!"; |
74 for (int i = 0; i < totalConnections; i++) { | 36 for (int i = 0; i < totalConnections; i++) { |
75 int messageCount = 0; | 37 int messageCount = 0; |
76 WebSocketClientConnection wsconn = createClient(server.port); | 38 WebSocket.connect("ws://127.0.0.1:${server.port}/") |
77 wsconn.onOpen = () => wsconn.send(messageText); | 39 .then((webSocket) { |
78 wsconn.onMessage = (message) { | 40 webSocket.send(messageText); |
79 messageCount++; | 41 webSocket.listen((event) { |
80 if (messageCount < 10) { | 42 if (event is MessageEvent) { |
81 Expect.equals(messageText, message); | 43 messageCount++; |
82 wsconn.send(message); | 44 if (messageCount < 1 ) { |
83 } else { | 45 Expect.equals(messageText, event.data); |
84 wsconn.close(closeStatus, closeReason); | 46 webSocket.send(event.data); |
85 } | 47 } else { |
86 }; | 48 webSocket.close(closeStatus, closeReason); |
87 wsconn.onClosed = (status, reason) { | 49 } |
88 Expect.equals(closeStatus == null | 50 } else if (event is CloseEvent) { |
89 ? WebSocketStatus.NO_STATUS_RECEIVED | 51 Expect.equals(closeStatus == null |
90 : closeStatus, status); | 52 ? WebSocketStatus.NO_STATUS_RECEIVED |
91 Expect.equals("", reason); | 53 : closeStatus, event.code); |
92 closeCount++; | 54 Expect.equals("", event.reason); |
93 if (closeCount == totalConnections) { | 55 closeCount++; |
94 client.shutdown(); | 56 if (closeCount == totalConnections) { |
95 server.close(); | 57 server.close(); |
96 } | 58 } |
97 }; | 59 } |
98 } | 60 }); |
99 } | 61 }); |
100 | 62 } |
101 | 63 |
102 void testRequestResponseServerCloses( | 64 }); |
103 int totalConnections, int closeStatus, String closeReason) { | 65 } |
104 ReceivePort keepAlive = new ReceivePort(); | 66 |
105 HttpServer server = createServer(backlog: totalConnections); | 67 |
106 | 68 void testRequestResponseServerCloses( |
107 // Create a web socket handler and set is as the HTTP server default | 69 int totalConnections, int closeStatus, String closeReason) { |
108 // handler. | 70 HttpServer.bind().then((server) { |
| 71 |
109 int closeCount = 0; | 72 int closeCount = 0; |
110 WebSocketHandler wsHandler = new WebSocketHandler(); | 73 server.transform(new WebSocketTransformer()).listen((webSocket) { |
111 wsHandler.onOpen = (WebSocketConnection conn) { | |
112 String messageText = "Hello, world!"; | 74 String messageText = "Hello, world!"; |
113 int messageCount = 0; | 75 int messageCount = 0; |
114 conn.onMessage = (Object message) { | 76 webSocket.listen((event) { |
115 messageCount++; | 77 if (event is MessageEvent) { |
116 if (messageCount < 10) { | 78 messageCount++; |
117 Expect.equals(messageText, message); | 79 if (messageCount < 10) { |
118 conn.send(message); | 80 Expect.equals(messageText, event.data); |
119 } else { | 81 webSocket.send(event.data); |
120 conn.close(closeStatus, closeReason); | 82 } else { |
121 } | 83 webSocket.close(closeStatus, closeReason); |
122 }; | 84 } |
123 conn.onClosed = (status, reason) { | 85 } else if (event is CloseEvent) { |
124 Expect.equals(closeStatus == null | 86 Expect.equals(closeStatus == null |
125 ? WebSocketStatus.NO_STATUS_RECEIVED | 87 ? WebSocketStatus.NO_STATUS_RECEIVED |
126 : closeStatus, status); | 88 : closeStatus, event.code); |
127 Expect.equals("", reason); | 89 Expect.equals("", event.reason); |
128 closeCount++; | 90 closeCount++; |
129 if (closeCount == totalConnections) { | 91 if (closeCount == totalConnections) { |
130 client.shutdown(); | 92 server.close(); |
131 server.close(); | 93 } |
132 keepAlive.close(); | 94 } |
133 } | 95 }); |
134 }; | 96 webSocket.send(messageText); |
135 conn.send(messageText); | 97 }); |
136 }; | |
137 server.defaultRequestHandler = wsHandler.onRequest; | |
138 | 98 |
139 for (int i = 0; i < totalConnections; i++) { | 99 for (int i = 0; i < totalConnections; i++) { |
140 WebSocketClientConnection wsconn = createClient(server.port); | 100 WebSocket.connect("ws://127.0.0.1:${server.port}/") |
141 wsconn.onMessage = (message) => wsconn.send(message); | 101 .then((webSocket) { |
142 wsconn.onClosed = (status, reason) { | 102 webSocket.listen((event) { |
143 Expect.equals(closeStatus == null | 103 if (event is MessageEvent) { |
144 ? WebSocketStatus.NO_STATUS_RECEIVED | 104 webSocket.send(event.data); |
145 : closeStatus, status); | 105 } else if (event is CloseEvent) { |
146 Expect.equals(closeReason == null ? "" : closeReason, reason); | 106 Expect.equals(closeStatus == null |
147 }; | 107 ? WebSocketStatus.NO_STATUS_RECEIVED |
148 } | 108 : closeStatus, event.code); |
149 } | 109 Expect.equals( |
150 | 110 closeReason == null ? "" : closeReason, event.reason); |
151 | 111 } |
152 void testMessageLength(int messageLength) { | 112 }); |
153 HttpServer server = createServer(backlog: 1); | 113 }); |
154 bool serverReceivedMessage = false; | 114 } |
155 bool clientReceivedMessage = false; | 115 |
156 | 116 }); |
157 // Create a web socket handler and set is as the HTTP server default | 117 } |
158 // handler. | 118 |
| 119 |
| 120 void testMessageLength(int messageLength) { |
| 121 HttpServer.bind().then((server) { |
| 122 |
159 Uint8List originalMessage = new Uint8List(messageLength); | 123 Uint8List originalMessage = new Uint8List(messageLength); |
160 WebSocketHandler wsHandler = new WebSocketHandler(); | 124 server.transform(new WebSocketTransformer()).listen((webSocket) { |
161 wsHandler.onOpen = (WebSocketConnection conn) { | 125 webSocket.listen((event) { |
162 conn.onMessage = (Object message) { | 126 if (event is MessageEvent) { |
163 serverReceivedMessage = true; | 127 Expect.listEquals(originalMessage, event.data); |
164 Expect.listEquals(originalMessage, message); | 128 webSocket.send(event.data); |
165 conn.send(message); | 129 } else if (event is CloseEvent) { |
166 }; | 130 } |
167 conn.onClosed = (status, reason) { | 131 }); |
168 }; | 132 }); |
169 }; | 133 |
170 server.defaultRequestHandler = wsHandler.onRequest; | 134 WebSocket.connect("ws://127.0.0.1:${server.port}/") |
171 | 135 .then((webSocket) { |
172 WebSocketClientConnection wsconn = createClient(server.port); | 136 webSocket.listen((event) { |
173 wsconn.onMessage = (message) { | 137 if (event is MessageEvent) { |
174 clientReceivedMessage = true; | 138 Expect.listEquals(originalMessage, event.data); |
175 Expect.listEquals(originalMessage, message); | 139 webSocket.close(); |
176 wsconn.close(); | 140 } else if (event is CloseEvent) { |
177 }; | 141 server.close(); |
178 wsconn.onClosed = (status, reason) { | 142 } |
179 Expect.isTrue(serverReceivedMessage); | 143 }); |
180 Expect.isTrue(clientReceivedMessage); | 144 webSocket.send(originalMessage); |
181 client.shutdown(); | 145 }); |
| 146 |
| 147 }); |
| 148 } |
| 149 |
| 150 |
| 151 void testNoUpgrade() { |
| 152 HttpServer.bind().then((server) { |
| 153 |
| 154 // Create a server which always responds with NOT_FOUND. |
| 155 server.listen((request) { |
| 156 request.response.statusCode = HttpStatus.NOT_FOUND; |
| 157 request.response.close(); |
| 158 }); |
| 159 |
| 160 WebSocket.connect("ws://127.0.0.1:${server.port}/").catchError((error) { |
182 server.close(); | 161 server.close(); |
183 }; | 162 }); |
184 wsconn.onOpen = () { | 163 |
185 wsconn.send(originalMessage); | 164 }); |
186 }; | 165 } |
187 } | 166 |
188 | 167 |
189 | 168 void testUsePOST() { |
190 void testNoUpgrade() { | 169 HttpServer.bind().then((server) { |
191 HttpServer server = createServer(backlog: 5); | 170 |
192 | 171 var errorPort = new ReceivePort(); |
193 // Create a server which always responds with a redirect. | 172 server.transform(new WebSocketTransformer()).listen((webSocket) { |
194 server.defaultRequestHandler = (request, response) { | 173 Expect.fail("No connection expected"); |
195 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | 174 }, onError: (e) { |
196 response.outputStream.close(); | 175 errorPort.close(); |
197 }; | 176 }); |
198 | 177 |
199 WebSocketClientConnection wsconn = createClient(server.port, | 178 HttpClient client = new HttpClient(); |
200 followRedirects: false); | 179 client.post("127.0.0.1", server.port, "/") |
201 wsconn.onNoUpgrade = (response) { | 180 .then((request) => request.close()) |
202 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | 181 .then((response) { |
203 client.shutdown(); | 182 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
204 server.close(); | 183 client.close(); |
205 }; | 184 server.close(); |
206 } | 185 }); |
207 | 186 |
208 | 187 }); |
209 void testUsePOST() { | 188 } |
210 HttpServer server = createServer(backlog: 5); | 189 |
211 | 190 |
212 // Create a web socket handler and set is as the HTTP server default | 191 class WebSocketInfo { |
213 // handler. | 192 int messageCount = 0; |
| 193 } |
| 194 |
| 195 |
| 196 void testW3CInterface( |
| 197 int totalConnections, int closeStatus, String closeReason) { |
| 198 HttpServer.bind().then((server) { |
| 199 |
214 int closeCount = 0; | 200 int closeCount = 0; |
215 WebSocketHandler wsHandler = new WebSocketHandler(); | 201 server.transform(new WebSocketTransformer()).listen((webSocket) { |
216 wsHandler.onOpen = (WebSocketConnection conn) { | |
217 Expect.fail("No connection expected"); | |
218 }; | |
219 server.defaultRequestHandler = wsHandler.onRequest; | |
220 | |
221 WebSocketClientConnection wsconn = createClient(server.port, | |
222 method: "POST"); | |
223 wsconn.onNoUpgrade = (response) { | |
224 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | |
225 client.shutdown(); | |
226 server.close(); | |
227 }; | |
228 } | |
229 | |
230 | |
231 void testHashCode(int totalConnections) { | |
232 ReceivePort keepAlive = new ReceivePort(); | |
233 HttpServer server = createServer(backlog: totalConnections); | |
234 Map connections = new Map(); | |
235 | |
236 void handleMessage(conn, message) { | |
237 var info = connections[conn]; | |
238 Expect.isNotNull(info); | |
239 info.messageCount++; | |
240 if (info.messageCount < 10) { | |
241 conn.send(message); | |
242 } else { | |
243 conn.close(); | |
244 } | |
245 } | |
246 | |
247 // Create a web socket handler and set is as the HTTP server default | |
248 // handler. | |
249 int closeCount = 0; | |
250 WebSocketHandler wsHandler = new WebSocketHandler(); | |
251 wsHandler.onOpen = (WebSocketConnection conn) { | |
252 connections[conn] = new WebSocketInfo(); | |
253 String messageText = "Hello, world!"; | |
254 conn.onMessage = (Object message) { | |
255 handleMessage(conn, message); | |
256 }; | |
257 conn.onClosed = (status, reason) { | |
258 closeCount++; | |
259 var info = connections[conn]; | |
260 Expect.equals(10, info.messageCount); | |
261 if (closeCount == totalConnections) { | |
262 client.shutdown(); | |
263 server.close(); | |
264 keepAlive.close(); | |
265 } | |
266 }; | |
267 conn.send(messageText); | |
268 }; | |
269 server.defaultRequestHandler = wsHandler.onRequest; | |
270 | |
271 for (int i = 0; i < totalConnections; i++) { | |
272 WebSocketClientConnection wsconn = createClient(server.port); | |
273 wsconn.onMessage = (message) => wsconn.send(message); | |
274 } | |
275 } | |
276 | |
277 | |
278 void testW3CInterface( | |
279 int totalConnections, int closeStatus, String closeReason) { | |
280 HttpServer server = createServer(backlog: totalConnections); | |
281 | |
282 // Create a web socket handler and set is as the HTTP server default | |
283 // handler. | |
284 int closeCount = 0; | |
285 WebSocketHandler wsHandler = new WebSocketHandler(); | |
286 wsHandler.onOpen = (WebSocketConnection conn) { | |
287 String messageText = "Hello, world!"; | 202 String messageText = "Hello, world!"; |
288 int messageCount = 0; | 203 int messageCount = 0; |
289 conn.onMessage = (Object message) { | 204 webSocket.listen((event) { |
290 messageCount++; | 205 if (event is MessageEvent) { |
291 if (messageCount < 10) { | 206 messageCount++; |
292 Expect.equals(messageText, message); | 207 if (messageCount < 10) { |
293 conn.send(message); | 208 Expect.equals(messageText, event.data); |
294 } else { | 209 webSocket.send(event.data); |
295 conn.close(closeStatus, closeReason); | 210 } else { |
296 } | 211 webSocket.close(closeStatus, closeReason); |
297 }; | 212 } |
298 conn.onClosed = (status, reason) { | 213 } else if (event is CloseEvent) { |
299 Expect.equals(closeStatus, status); | 214 Expect.equals(closeStatus, event.code); |
300 Expect.equals("", reason); | 215 Expect.equals("", event.reason); |
301 closeCount++; | 216 closeCount++; |
302 if (closeCount == totalConnections) { | 217 if (closeCount == totalConnections) { |
303 server.close(); | 218 server.close(); |
304 } | 219 } |
305 }; | 220 } |
306 conn.send(messageText); | 221 }); |
307 }; | 222 webSocket.send(messageText); |
308 server.defaultRequestHandler = wsHandler.onRequest; | 223 }); |
309 | 224 |
310 void webSocketConnection() { | 225 void webSocketConnection() { |
311 bool onopenCalled = false; | 226 bool onopenCalled = false; |
312 int onmessageCalled = 0; | 227 int onmessageCalled = 0; |
313 bool oncloseCalled = false; | 228 bool oncloseCalled = false; |
314 | 229 |
315 var websocket = | 230 WebSocket.connect("ws://127.0.0.1:${server.port}").then((webSocket) { |
316 new WebSocket('${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}'); | |
317 Expect.equals(WebSocket.CONNECTING, websocket.readyState); | |
318 websocket.onopen = () { | |
319 Expect.isFalse(onopenCalled); | 231 Expect.isFalse(onopenCalled); |
320 Expect.equals(0, onmessageCalled); | 232 Expect.equals(0, onmessageCalled); |
321 Expect.isFalse(oncloseCalled); | 233 Expect.isFalse(oncloseCalled); |
322 onopenCalled = true; | 234 onopenCalled = true; |
323 Expect.equals(WebSocket.OPEN, websocket.readyState); | 235 Expect.equals(WebSocket.OPEN, webSocket.readyState); |
324 }; | 236 webSocket.listen((event) { |
325 websocket.onmessage = (event) { | 237 if (event is MessageEvent) { |
326 onmessageCalled++; | 238 onmessageCalled++; |
327 Expect.isTrue(onopenCalled); | 239 Expect.isTrue(onopenCalled); |
328 Expect.isFalse(oncloseCalled); | 240 Expect.isFalse(oncloseCalled); |
329 Expect.equals(WebSocket.OPEN, websocket.readyState); | 241 Expect.equals(WebSocket.OPEN, webSocket.readyState); |
330 websocket.send(event.data); | 242 webSocket.send(event.data); |
331 }; | 243 } else if (event is CloseEvent) { |
332 websocket.onclose = (event) { | 244 Expect.isTrue(onopenCalled); |
333 Expect.isTrue(onopenCalled); | 245 Expect.equals(10, onmessageCalled); |
334 Expect.equals(10, onmessageCalled); | 246 Expect.isFalse(oncloseCalled); |
335 Expect.isFalse(oncloseCalled); | 247 oncloseCalled = true; |
336 oncloseCalled = true; | 248 Expect.isTrue(event.wasClean); |
337 Expect.isTrue(event.wasClean); | 249 Expect.equals(3002, event.code); |
338 Expect.equals(3002, event.code); | 250 Expect.equals("Got tired", event.reason); |
339 Expect.equals("Got tired", event.reason); | 251 Expect.equals(WebSocket.CLOSED, webSocket.readyState); |
340 Expect.equals(WebSocket.CLOSED, websocket.readyState); | 252 } |
341 }; | 253 }); |
| 254 }); |
342 } | 255 } |
343 | 256 |
344 for (int i = 0; i < totalConnections; i++) { | 257 for (int i = 0; i < totalConnections; i++) { |
345 webSocketConnection(); | 258 webSocketConnection(); |
346 } | 259 } |
347 } | 260 |
348 | 261 }); |
349 | |
350 runTests() { | |
351 testRequestResponseClientCloses(2, null, null); | |
352 testRequestResponseClientCloses(2, 3001, null); | |
353 testRequestResponseClientCloses(2, 3002, "Got tired"); | |
354 testRequestResponseServerCloses(2, null, null); | |
355 testRequestResponseServerCloses(2, 3001, null); | |
356 testRequestResponseServerCloses(2, 3002, "Got tired"); | |
357 testMessageLength(125); | |
358 testMessageLength(126); | |
359 testMessageLength(127); | |
360 testMessageLength(65535); | |
361 testMessageLength(65536); | |
362 testNoUpgrade(); | |
363 testUsePOST(); | |
364 testHashCode(2); | |
365 testW3CInterface(2, 3002, "Got tired"); | |
366 } | |
367 } | |
368 | |
369 | |
370 void initializeSSL() { | |
371 var testPkcertDatabase = | |
372 new Path(new Options().script).directoryPath.append("pkcert/"); | |
373 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | |
374 password: "dartdart"); | |
375 } | 262 } |
376 | 263 |
377 | 264 |
378 main() { | 265 main() { |
379 new SecurityConfiguration(secure: false).runTests(); | 266 testRequestResponseClientCloses(2, null, null); |
380 } | 267 testRequestResponseClientCloses(2, 3001, null); |
| 268 testRequestResponseClientCloses(2, 3002, "Got tired"); |
| 269 testRequestResponseServerCloses(2, null, null); |
| 270 testRequestResponseServerCloses(2, 3001, null); |
| 271 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 272 testMessageLength(125); |
| 273 testMessageLength(126); |
| 274 testMessageLength(127); |
| 275 testMessageLength(65535); |
| 276 testMessageLength(65536); |
| 277 testNoUpgrade(); |
| 278 testUsePOST(); |
| 279 |
| 280 testW3CInterface(2, 3002, "Got tired"); |
| 281 } |
OLD | NEW |