Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, 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= | 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 | 9 |
| 10 // TODO(7157): Remove this test once the bug is fixed. | 10 // TODO(7157): Remove this test once the bug is fixed. |
| 11 // 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 |
| 12 // tests disabled, so it does not crash on Windows. | 12 // tests disabled, so it does not crash on Windows. |
| 13 | |
| 13 import "dart:io"; | 14 import "dart:io"; |
| 14 import "dart:isolate"; | 15 import "dart:isolate"; |
| 15 import "dart:scalarlist"; | 16 import "dart:scalarlist"; |
| 17 import "dart:uri"; | |
| 16 | 18 |
| 17 void testRequestResponseClientCloses( | 19 const String CERT_NAME = 'localhost_cert'; |
| 18 int totalConnections, int closeStatus, String closeReason) { | 20 const String SERVER_ADDRESS = '127.0.0.1'; |
| 19 HttpServer.bind().then((server) { | 21 const String HOST_NAME = 'localhost'; |
| 20 | 22 |
| 23 /** | |
| 24 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. | |
| 25 */ | |
| 26 class SecurityConfiguration { | |
| 27 final bool secure; | |
| 28 | |
| 29 SecurityConfiguration({bool this.secure}); | |
| 30 | |
| 31 Future<HttpServer> createServer({int backlog: 0}) => | |
| 32 secure ? HttpServer.bindSecure(SERVER_ADDRESS, | |
| 33 0, | |
| 34 backlog: backlog, | |
| 35 certificateName: CERT_NAME) | |
| 36 : HttpServer.bind(SERVER_ADDRESS, | |
| 37 0, | |
| 38 backlog); | |
| 39 | |
| 40 Future<WebSocket> createClient(int port) => | |
| 41 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); | |
| 42 | |
| 43 void testRequestResponseClientCloses(int totalConnections, | |
| 44 int closeStatus, | |
| 45 String closeReason) { | |
| 46 createServer().then((server) { | |
| 21 server.transform(new WebSocketTransformer()).listen((webSocket) { | 47 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 22 webSocket.listen((event) { | 48 webSocket.listen((event) { |
| 23 if (event is MessageEvent) { | 49 if (event is MessageEvent) { |
| 24 webSocket.send(event.data); | 50 webSocket.send(event.data); |
| 25 } else if (event is CloseEvent) { | 51 } else if (event is CloseEvent) { |
| 26 Expect.equals(closeStatus == null | 52 Expect.equals(closeStatus == null |
| 27 ? WebSocketStatus.NO_STATUS_RECEIVED | 53 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 28 : closeStatus, event.code); | 54 : closeStatus, event.code); |
| 29 Expect.equals(closeReason == null ? "" : closeReason, event.reason); | 55 Expect.equals(closeReason == null ? "" : closeReason, event.reason); |
| 30 } | 56 } |
| 31 }); | 57 }); |
| 32 }); | 58 }); |
| 33 | 59 |
| 34 int closeCount = 0; | 60 int closeCount = 0; |
| 35 String messageText = "Hello, world!"; | 61 String messageText = "Hello, world!"; |
| 36 for (int i = 0; i < totalConnections; i++) { | 62 for (int i = 0; i < totalConnections; i++) { |
| 37 int messageCount = 0; | 63 int messageCount = 0; |
| 38 WebSocket.connect("ws://127.0.0.1:${server.port}/") | 64 createClient(server.port).then((webSocket) { |
| 39 .then((webSocket) { | 65 webSocket.send(messageText); |
|
Bill Hesse
2013/02/26 14:56:43
This is also just an indentation change, from 65 t
Søren Gjesse
2013/02/26 15:28:51
Thanks.
| |
| 40 webSocket.send(messageText); | 66 webSocket.listen((event) { |
| 41 webSocket.listen((event) { | 67 if (event is MessageEvent) { |
| 42 if (event is MessageEvent) { | 68 messageCount++; |
| 43 messageCount++; | 69 if (messageCount < 1 ) { |
| 44 if (messageCount < 1 ) { | 70 Expect.equals(messageText, event.data); |
| 45 Expect.equals(messageText, event.data); | 71 webSocket.send(event.data); |
| 46 webSocket.send(event.data); | 72 } else { |
| 47 } else { | 73 webSocket.close(closeStatus, closeReason); |
| 48 webSocket.close(closeStatus, closeReason); | |
| 49 } | |
| 50 } else if (event is CloseEvent) { | |
| 51 Expect.equals(closeStatus == null | |
| 52 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 53 : closeStatus, event.code); | |
| 54 Expect.equals("", event.reason); | |
| 55 closeCount++; | |
| 56 if (closeCount == totalConnections) { | |
| 57 server.close(); | |
| 58 } | |
| 59 } | 74 } |
| 60 }); | 75 } else if (event is CloseEvent) { |
| 76 Expect.equals(closeStatus == null | |
| 77 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 78 : closeStatus, event.code); | |
| 79 Expect.equals("", event.reason); | |
| 80 closeCount++; | |
| 81 if (closeCount == totalConnections) { | |
| 82 server.close(); | |
| 83 } | |
| 84 } | |
| 61 }); | 85 }); |
| 86 }); | |
| 62 } | 87 } |
| 63 | |
| 64 }); | 88 }); |
| 65 } | 89 } |
| 66 | 90 |
| 67 | 91 void testRequestResponseServerCloses(int totalConnections, |
| 68 void testRequestResponseServerCloses( | 92 int closeStatus, |
| 69 int totalConnections, int closeStatus, String closeReason) { | 93 String closeReason) { |
| 70 HttpServer.bind().then((server) { | 94 createServer().then((server) { |
| 71 | |
| 72 int closeCount = 0; | 95 int closeCount = 0; |
| 73 server.transform(new WebSocketTransformer()).listen((webSocket) { | 96 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 74 String messageText = "Hello, world!"; | 97 String messageText = "Hello, world!"; |
| 75 int messageCount = 0; | 98 int messageCount = 0; |
| 76 webSocket.listen((event) { | 99 webSocket.listen((event) { |
| 77 if (event is MessageEvent) { | 100 if (event is MessageEvent) { |
| 78 messageCount++; | 101 messageCount++; |
| 79 if (messageCount < 10) { | 102 if (messageCount < 10) { |
| 80 Expect.equals(messageText, event.data); | 103 Expect.equals(messageText, event.data); |
| 81 webSocket.send(event.data); | 104 webSocket.send(event.data); |
| 82 } else { | 105 } else { |
| 83 webSocket.close(closeStatus, closeReason); | 106 webSocket.close(closeStatus, closeReason); |
| 84 } | 107 } |
| 85 } else if (event is CloseEvent) { | 108 } else if (event is CloseEvent) { |
| 86 Expect.equals(closeStatus == null | 109 Expect.equals(closeStatus == null |
| 87 ? WebSocketStatus.NO_STATUS_RECEIVED | 110 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 88 : closeStatus, event.code); | 111 : closeStatus, event.code); |
| 89 Expect.equals("", event.reason); | 112 Expect.equals("", event.reason); |
| 90 closeCount++; | 113 closeCount++; |
| 91 if (closeCount == totalConnections) { | 114 if (closeCount == totalConnections) { |
| 92 server.close(); | 115 server.close(); |
| 93 } | 116 } |
| 94 } | 117 } |
| 95 }); | 118 }); |
| 96 webSocket.send(messageText); | 119 webSocket.send(messageText); |
| 97 }); | 120 }); |
| 98 | 121 |
| 99 for (int i = 0; i < totalConnections; i++) { | 122 for (int i = 0; i < totalConnections; i++) { |
| 100 WebSocket.connect("ws://127.0.0.1:${server.port}/") | 123 createClient(server.port).then((webSocket) { |
| 101 .then((webSocket) { | |
| 102 webSocket.listen((event) { | 124 webSocket.listen((event) { |
| 103 if (event is MessageEvent) { | 125 if (event is MessageEvent) { |
| 104 webSocket.send(event.data); | 126 webSocket.send(event.data); |
| 105 } else if (event is CloseEvent) { | 127 } else if (event is CloseEvent) { |
| 106 Expect.equals(closeStatus == null | 128 Expect.equals(closeStatus == null |
| 107 ? WebSocketStatus.NO_STATUS_RECEIVED | 129 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 108 : closeStatus, event.code); | 130 : closeStatus, event.code); |
| 109 Expect.equals( | 131 Expect.equals( |
| 110 closeReason == null ? "" : closeReason, event.reason); | 132 closeReason == null ? "" : closeReason, event.reason); |
| 111 } | 133 } |
| 112 }); | 134 }); |
| 113 }); | 135 }); |
| 114 } | 136 } |
| 115 | 137 |
| 116 }); | 138 }); |
| 117 } | 139 } |
| 118 | 140 |
| 119 | 141 |
| 120 void testMessageLength(int messageLength) { | 142 void testMessageLength(int messageLength) { |
| 121 HttpServer.bind().then((server) { | 143 createServer().then((server) { |
| 122 | |
| 123 Uint8List originalMessage = new Uint8List(messageLength); | 144 Uint8List originalMessage = new Uint8List(messageLength); |
| 124 server.transform(new WebSocketTransformer()).listen((webSocket) { | 145 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 125 webSocket.listen((event) { | 146 webSocket.listen((event) { |
| 126 if (event is MessageEvent) { | 147 if (event is MessageEvent) { |
| 127 Expect.listEquals(originalMessage, event.data); | 148 Expect.listEquals(originalMessage, event.data); |
| 128 webSocket.send(event.data); | 149 webSocket.send(event.data); |
| 129 } else if (event is CloseEvent) { | 150 } else if (event is CloseEvent) { |
| 130 } | 151 } |
| 131 }); | 152 }); |
| 132 }); | 153 }); |
| 133 | 154 |
| 134 WebSocket.connect("ws://127.0.0.1:${server.port}/") | 155 createClient(server.port).then((webSocket) { |
| 135 .then((webSocket) { | |
| 136 webSocket.listen((event) { | 156 webSocket.listen((event) { |
| 137 if (event is MessageEvent) { | 157 if (event is MessageEvent) { |
| 138 Expect.listEquals(originalMessage, event.data); | 158 Expect.listEquals(originalMessage, event.data); |
| 139 webSocket.close(); | 159 webSocket.close(); |
| 140 } else if (event is CloseEvent) { | 160 } else if (event is CloseEvent) { |
| 141 server.close(); | 161 server.close(); |
| 142 } | 162 } |
| 143 }); | 163 }); |
| 144 webSocket.send(originalMessage); | 164 webSocket.send(originalMessage); |
| 145 }); | 165 }); |
| 146 | |
| 147 }); | 166 }); |
| 148 } | 167 } |
| 149 | 168 |
| 150 | 169 |
| 151 void testNoUpgrade() { | 170 void testNoUpgrade() { |
| 152 HttpServer.bind().then((server) { | 171 createServer().then((server) { |
| 153 | |
| 154 // Create a server which always responds with NOT_FOUND. | 172 // Create a server which always responds with NOT_FOUND. |
| 155 server.listen((request) { | 173 server.listen((request) { |
| 156 request.response.statusCode = HttpStatus.NOT_FOUND; | 174 request.response.statusCode = HttpStatus.NOT_FOUND; |
| 157 request.response.close(); | 175 request.response.close(); |
| 158 }); | 176 }); |
| 159 | 177 |
| 160 WebSocket.connect("ws://127.0.0.1:${server.port}/").catchError((error) { | 178 createClient(server.port).catchError((error) { |
| 161 server.close(); | 179 server.close(); |
| 162 }); | 180 }); |
| 163 | |
| 164 }); | 181 }); |
| 165 } | 182 } |
| 166 | 183 |
| 167 | 184 |
| 168 void testUsePOST() { | 185 void testUsePOST() { |
| 169 HttpServer.bind().then((server) { | 186 createServer().then((server) { |
| 170 | |
| 171 var errorPort = new ReceivePort(); | 187 var errorPort = new ReceivePort(); |
| 172 server.transform(new WebSocketTransformer()).listen((webSocket) { | 188 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 173 Expect.fail("No connection expected"); | 189 Expect.fail("No connection expected"); |
| 174 }, onError: (e) { | 190 }, onError: (e) { |
| 175 errorPort.close(); | 191 errorPort.close(); |
| 176 }); | 192 }); |
| 177 | 193 |
| 178 HttpClient client = new HttpClient(); | 194 HttpClient client = new HttpClient(); |
| 179 client.post("127.0.0.1", server.port, "/") | 195 client.postUrl(Uri.parse( |
| 196 "${secure ? 'https:' : 'http:'}//$HOST_NAME:${server.port}/")) | |
| 180 .then((request) => request.close()) | 197 .then((request) => request.close()) |
| 181 .then((response) { | 198 .then((response) { |
| 182 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 199 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 183 client.close(); | 200 client.close(); |
| 184 server.close(); | 201 server.close(); |
| 185 }); | 202 }); |
| 186 | |
| 187 }); | 203 }); |
| 188 } | 204 } |
| 189 | 205 |
| 190 | 206 void testW3CInterface(int totalConnections, |
|
Søren Gjesse
2013/02/26 15:28:51
Do we need this separate test anymore now that thi
Bill Hesse
2013/02/26 16:09:50
It is written differently, and tests different thi
| |
| 191 class WebSocketInfo { | 207 int closeStatus, |
| 192 int messageCount = 0; | 208 String closeReason) { |
| 193 } | 209 createServer().then((server) { |
| 194 | |
| 195 | |
| 196 void testW3CInterface( | |
| 197 int totalConnections, int closeStatus, String closeReason) { | |
| 198 HttpServer.bind().then((server) { | |
| 199 | |
| 200 int closeCount = 0; | 210 int closeCount = 0; |
| 201 server.transform(new WebSocketTransformer()).listen((webSocket) { | 211 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 202 String messageText = "Hello, world!"; | 212 String messageText = "Hello, world!"; |
| 203 int messageCount = 0; | 213 int messageCount = 0; |
| 204 webSocket.listen((event) { | 214 webSocket.listen((event) { |
| 205 if (event is MessageEvent) { | 215 if (event is MessageEvent) { |
| 206 messageCount++; | 216 messageCount++; |
| 207 if (messageCount < 10) { | 217 if (messageCount < 10) { |
| 208 Expect.equals(messageText, event.data); | 218 Expect.equals(messageText, event.data); |
| 209 webSocket.send(event.data); | 219 webSocket.send(event.data); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 220 } | 230 } |
| 221 }); | 231 }); |
| 222 webSocket.send(messageText); | 232 webSocket.send(messageText); |
| 223 }); | 233 }); |
| 224 | 234 |
| 225 void webSocketConnection() { | 235 void webSocketConnection() { |
| 226 bool onopenCalled = false; | 236 bool onopenCalled = false; |
| 227 int onmessageCalled = 0; | 237 int onmessageCalled = 0; |
| 228 bool oncloseCalled = false; | 238 bool oncloseCalled = false; |
| 229 | 239 |
| 230 WebSocket.connect("ws://127.0.0.1:${server.port}").then((webSocket) { | 240 createClient(server.port).then((webSocket) { |
| 231 Expect.isFalse(onopenCalled); | 241 Expect.isFalse(onopenCalled); |
| 232 Expect.equals(0, onmessageCalled); | 242 Expect.equals(0, onmessageCalled); |
| 233 Expect.isFalse(oncloseCalled); | 243 Expect.isFalse(oncloseCalled); |
| 234 onopenCalled = true; | 244 onopenCalled = true; |
| 235 Expect.equals(WebSocket.OPEN, webSocket.readyState); | 245 Expect.equals(WebSocket.OPEN, webSocket.readyState); |
| 236 webSocket.listen((event) { | 246 webSocket.listen((event) { |
| 237 if (event is MessageEvent) { | 247 if (event is MessageEvent) { |
| 238 onmessageCalled++; | 248 onmessageCalled++; |
| 239 Expect.isTrue(onopenCalled); | 249 Expect.isTrue(onopenCalled); |
| 240 Expect.isFalse(oncloseCalled); | 250 Expect.isFalse(oncloseCalled); |
| 241 Expect.equals(WebSocket.OPEN, webSocket.readyState); | 251 Expect.equals(WebSocket.OPEN, webSocket.readyState); |
| 242 webSocket.send(event.data); | 252 webSocket.send(event.data); |
| 243 } else if (event is CloseEvent) { | 253 } else if (event is CloseEvent) { |
| 244 Expect.isTrue(onopenCalled); | 254 Expect.isTrue(onopenCalled); |
| 245 Expect.equals(10, onmessageCalled); | 255 Expect.equals(10, onmessageCalled); |
| 246 Expect.isFalse(oncloseCalled); | 256 Expect.isFalse(oncloseCalled); |
| 247 oncloseCalled = true; | 257 oncloseCalled = true; |
| 248 Expect.isTrue(event.wasClean); | 258 Expect.isTrue(event.wasClean); |
| 249 Expect.equals(3002, event.code); | 259 Expect.equals(3002, event.code); |
| 250 Expect.equals("Got tired", event.reason); | 260 Expect.equals("Got tired", event.reason); |
| 251 Expect.equals(WebSocket.CLOSED, webSocket.readyState); | 261 Expect.equals(WebSocket.CLOSED, webSocket.readyState); |
| 252 } | 262 } |
| 253 }); | 263 }); |
| 254 }); | 264 }); |
| 255 } | 265 } |
| 256 | 266 |
| 257 for (int i = 0; i < totalConnections; i++) { | 267 for (int i = 0; i < totalConnections; i++) { |
| 258 webSocketConnection(); | 268 webSocketConnection(); |
| 259 } | 269 } |
| 270 }); | |
| 271 } | |
| 260 | 272 |
| 261 }); | 273 void runTests() { |
| 274 testRequestResponseClientCloses(2, null, null); | |
| 275 testRequestResponseClientCloses(2, 3001, null); | |
| 276 testRequestResponseClientCloses(2, 3002, "Got tired"); | |
| 277 testRequestResponseServerCloses(2, null, null); | |
| 278 testRequestResponseServerCloses(2, 3001, null); | |
| 279 testRequestResponseServerCloses(2, 3002, "Got tired"); | |
| 280 testMessageLength(125); | |
| 281 testMessageLength(126); | |
| 282 testMessageLength(127); | |
| 283 testMessageLength(65535); | |
| 284 testMessageLength(65536); | |
| 285 testNoUpgrade(); | |
| 286 testUsePOST(); | |
| 287 testW3CInterface(2, 3002, "Got tired"); | |
| 288 } | |
| 262 } | 289 } |
| 263 | 290 |
| 264 | 291 |
| 265 main() { | 292 main() { |
| 266 testRequestResponseClientCloses(2, null, null); | 293 new SecurityConfiguration(secure: false).runTests(); |
| 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 } | 294 } |
| OLD | NEW |