Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 5 |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 7 import "dart:io"; | 7 import "dart:io"; |
| 8 import "dart:isolate"; | 8 import "dart:isolate"; |
| 9 import "dart:scalarlist"; | 9 import "dart:scalarlist"; |
| 10 import "dart:uri"; | 10 import "dart:uri"; |
| 11 | 11 |
| 12 const SERVER_ADDRESS = "127.0.0.1"; | 12 const SERVER_ADDRESS = "127.0.0.1"; |
| 13 const HOST_NAME = "localhost"; | 13 const HOST_NAME = "localhost"; |
| 14 | 14 |
| 15 // We will run the tests once over HTTP and once over HTTPS. | 15 // We will run the tests once over HTTP and once over HTTPS. |
| 16 bool secure = false; | 16 bool secure = false; |
| 17 | 17 |
| 18 Future testRequestResponseClientCloses( | 18 void testRequestResponseClientCloses( |
| 19 int totalConnections, int closeStatus, String closeReason) { | 19 int totalConnections, int closeStatus, String closeReason) { |
| 20 Completer done = new Completer(); | 20 ReceivePort keepAlive = new ReceivePort(); |
|
Anders Johnsen
2013/01/17 14:17:33
The ReceivePorts are redundant in some of these te
| |
| 21 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 21 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
|
Anders Johnsen
2013/01/17 14:17:33
Move all of this out to a
createServer();
ditto
| |
| 22 HttpClient client = new HttpClient(); | 22 HttpClient client = new HttpClient(); |
| 23 | 23 |
| 24 server.listen(SERVER_ADDRESS, | 24 server.listen(SERVER_ADDRESS, |
| 25 0, | 25 0, |
| 26 backlog: totalConnections, | 26 backlog: totalConnections, |
| 27 certificate_name: "CN=$HOST_NAME"); | 27 certificate_name: "CN=$HOST_NAME"); |
| 28 | 28 |
| 29 // Create a web socket handler and set it as the HTTP server default handler. | 29 // Create a web socket handler and set it as the HTTP server default handler. |
| 30 WebSocketHandler wsHandler = new WebSocketHandler(); | 30 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 31 wsHandler.onOpen = (WebSocketConnection conn) { | 31 wsHandler.onOpen = (WebSocketConnection conn) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 59 }; | 59 }; |
| 60 wsconn.onClosed = (status, reason) { | 60 wsconn.onClosed = (status, reason) { |
| 61 Expect.equals(closeStatus == null | 61 Expect.equals(closeStatus == null |
| 62 ? WebSocketStatus.NO_STATUS_RECEIVED | 62 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 63 : closeStatus, status); | 63 : closeStatus, status); |
| 64 Expect.equals("", reason); | 64 Expect.equals("", reason); |
| 65 closeCount++; | 65 closeCount++; |
| 66 if (closeCount == totalConnections) { | 66 if (closeCount == totalConnections) { |
| 67 client.shutdown(); | 67 client.shutdown(); |
| 68 server.close(); | 68 server.close(); |
| 69 done.complete(null); | 69 keepAlive.close(); |
| 70 } | 70 } |
| 71 }; | 71 }; |
| 72 } | 72 } |
| 73 return done.future; | |
| 74 } | 73 } |
| 75 | 74 |
| 76 | 75 |
| 77 Future testRequestResponseServerCloses( | 76 void testRequestResponseServerCloses( |
| 78 int totalConnections, int closeStatus, String closeReason) { | 77 int totalConnections, int closeStatus, String closeReason) { |
| 79 Completer done = new Completer(); | 78 ReceivePort keepAlive = new ReceivePort(); |
| 80 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 79 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 81 HttpClient client = new HttpClient(); | 80 HttpClient client = new HttpClient(); |
| 82 | 81 |
| 83 server.listen(SERVER_ADDRESS, | 82 server.listen(SERVER_ADDRESS, |
| 84 0, | 83 0, |
| 85 backlog: totalConnections, | 84 backlog: totalConnections, |
| 86 certificate_name: "CN=$HOST_NAME"); | 85 certificate_name: "CN=$HOST_NAME"); |
| 87 | 86 |
| 88 // Create a web socket handler and set is as the HTTP server default | 87 // Create a web socket handler and set is as the HTTP server default |
| 89 // handler. | 88 // handler. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 103 }; | 102 }; |
| 104 conn.onClosed = (status, reason) { | 103 conn.onClosed = (status, reason) { |
| 105 Expect.equals(closeStatus == null | 104 Expect.equals(closeStatus == null |
| 106 ? WebSocketStatus.NO_STATUS_RECEIVED | 105 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 107 : closeStatus, status); | 106 : closeStatus, status); |
| 108 Expect.equals("", reason); | 107 Expect.equals("", reason); |
| 109 closeCount++; | 108 closeCount++; |
| 110 if (closeCount == totalConnections) { | 109 if (closeCount == totalConnections) { |
| 111 client.shutdown(); | 110 client.shutdown(); |
| 112 server.close(); | 111 server.close(); |
| 113 done.complete(null); | 112 keepAlive.close(); |
| 114 } | 113 } |
| 115 }; | 114 }; |
| 116 conn.send(messageText); | 115 conn.send(messageText); |
| 117 }; | 116 }; |
| 118 server.defaultRequestHandler = wsHandler.onRequest; | 117 server.defaultRequestHandler = wsHandler.onRequest; |
| 119 | 118 |
| 120 for (int i = 0; i < totalConnections; i++) { | 119 for (int i = 0; i < totalConnections; i++) { |
| 121 HttpClientConnection conn = client.getUrl(new Uri.fromString( | 120 HttpClientConnection conn = client.getUrl(new Uri.fromString( |
| 122 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | 121 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); |
| 123 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 122 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 124 wsconn.onMessage = (message) => wsconn.send(message); | 123 wsconn.onMessage = (message) => wsconn.send(message); |
| 125 wsconn.onClosed = (status, reason) { | 124 wsconn.onClosed = (status, reason) { |
| 126 Expect.equals(closeStatus == null | 125 Expect.equals(closeStatus == null |
| 127 ? WebSocketStatus.NO_STATUS_RECEIVED | 126 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 128 : closeStatus, status); | 127 : closeStatus, status); |
| 129 Expect.equals(closeReason == null ? "" : closeReason, reason); | 128 Expect.equals(closeReason == null ? "" : closeReason, reason); |
| 130 }; | 129 }; |
| 131 } | 130 } |
| 132 return done.future; | |
| 133 } | 131 } |
| 134 | 132 |
| 135 | 133 |
| 136 Future testMessageLength(int messageLength) { | 134 void testMessageLength(int messageLength) { |
| 137 Completer done = new Completer(); | 135 ReceivePort keepAlive = new ReceivePort(); |
| 138 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 136 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 139 HttpClient client = new HttpClient(); | 137 HttpClient client = new HttpClient(); |
| 140 bool serverReceivedMessage = false; | 138 bool serverReceivedMessage = false; |
| 141 bool clientReceivedMessage = false; | 139 bool clientReceivedMessage = false; |
| 142 | 140 |
| 143 server.listen(SERVER_ADDRESS, | 141 server.listen(SERVER_ADDRESS, |
| 144 0, | 142 0, |
| 145 backlog: 1, | 143 backlog: 1, |
| 146 certificate_name: "CN=$HOST_NAME"); | 144 certificate_name: "CN=$HOST_NAME"); |
| 147 | 145 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 166 wsconn.onMessage = (message) { | 164 wsconn.onMessage = (message) { |
| 167 clientReceivedMessage = true; | 165 clientReceivedMessage = true; |
| 168 Expect.listEquals(originalMessage, message); | 166 Expect.listEquals(originalMessage, message); |
| 169 wsconn.close(); | 167 wsconn.close(); |
| 170 }; | 168 }; |
| 171 wsconn.onClosed = (status, reason) { | 169 wsconn.onClosed = (status, reason) { |
| 172 Expect.isTrue(serverReceivedMessage); | 170 Expect.isTrue(serverReceivedMessage); |
| 173 Expect.isTrue(clientReceivedMessage); | 171 Expect.isTrue(clientReceivedMessage); |
| 174 client.shutdown(); | 172 client.shutdown(); |
| 175 server.close(); | 173 server.close(); |
| 176 done.complete(null); | 174 keepAlive.close(); |
| 177 }; | 175 }; |
| 178 wsconn.onOpen = () { | 176 wsconn.onOpen = () { |
| 179 wsconn.send(originalMessage); | 177 wsconn.send(originalMessage); |
| 180 }; | 178 }; |
| 181 return done.future; | |
| 182 } | 179 } |
| 183 | 180 |
| 184 | 181 |
| 185 Future testNoUpgrade() { | 182 void testNoUpgrade() { |
| 186 Completer done = new Completer(); | 183 ReceivePort keepAlive = new ReceivePort(); |
| 187 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 184 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 188 HttpClient client = new HttpClient(); | 185 HttpClient client = new HttpClient(); |
| 189 | 186 |
| 190 server.listen(SERVER_ADDRESS, | 187 server.listen(SERVER_ADDRESS, |
| 191 0, | 188 0, |
| 192 backlog: 5, | 189 backlog: 5, |
| 193 certificate_name: "CN=$HOST_NAME"); | 190 certificate_name: "CN=$HOST_NAME"); |
| 194 | 191 |
| 195 // Create a server which always responds with a redirect. | 192 // Create a server which always responds with a redirect. |
| 196 server.defaultRequestHandler = (request, response) { | 193 server.defaultRequestHandler = (request, response) { |
| 197 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | 194 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 198 response.outputStream.close(); | 195 response.outputStream.close(); |
| 199 }; | 196 }; |
| 200 | 197 |
| 201 HttpClientConnection conn = client.getUrl(new Uri.fromString( | 198 HttpClientConnection conn = client.getUrl(new Uri.fromString( |
| 202 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | 199 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); |
| 203 conn.followRedirects = false; | 200 conn.followRedirects = false; |
| 204 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 201 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 205 wsconn.onNoUpgrade = (response) { | 202 wsconn.onNoUpgrade = (response) { |
| 206 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | 203 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 207 client.shutdown(); | 204 client.shutdown(); |
| 208 server.close(); | 205 server.close(); |
| 209 done.complete(null); | 206 keepAlive.close(); |
| 210 }; | 207 }; |
| 211 return done.future; | |
| 212 } | 208 } |
| 213 | 209 |
| 214 | 210 |
| 215 Future testUsePOST() { | 211 void testUsePOST() { |
| 216 Completer done = new Completer(); | 212 ReceivePort keepAlive = new ReceivePort(); |
| 217 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 213 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 218 HttpClient client = new HttpClient(); | 214 HttpClient client = new HttpClient(); |
| 219 | 215 |
| 220 server.listen(SERVER_ADDRESS, | 216 server.listen(SERVER_ADDRESS, |
| 221 0, | 217 0, |
| 222 backlog: 5, | 218 backlog: 5, |
| 223 certificate_name: "CN=$HOST_NAME"); | 219 certificate_name: "CN=$HOST_NAME"); |
| 224 | 220 |
| 225 // Create a web socket handler and set is as the HTTP server default | 221 // Create a web socket handler and set is as the HTTP server default |
| 226 // handler. | 222 // handler. |
| 227 int closeCount = 0; | 223 int closeCount = 0; |
| 228 WebSocketHandler wsHandler = new WebSocketHandler(); | 224 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 229 wsHandler.onOpen = (WebSocketConnection conn) { | 225 wsHandler.onOpen = (WebSocketConnection conn) { |
| 230 Expect.fail("No connection expected"); | 226 Expect.fail("No connection expected"); |
| 231 }; | 227 }; |
| 232 server.defaultRequestHandler = wsHandler.onRequest; | 228 server.defaultRequestHandler = wsHandler.onRequest; |
| 233 | 229 |
| 234 HttpClientConnection conn = client.postUrl(new Uri.fromString( | 230 HttpClientConnection conn = client.postUrl(new Uri.fromString( |
| 235 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | 231 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); |
| 236 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 232 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 237 wsconn.onNoUpgrade = (response) { | 233 wsconn.onNoUpgrade = (response) { |
| 238 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 234 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 239 client.shutdown(); | 235 client.shutdown(); |
| 240 server.close(); | 236 server.close(); |
| 241 done.complete(null); | 237 keepAlive.close(); |
| 242 }; | 238 }; |
| 243 return done.future; | |
| 244 } | 239 } |
| 245 | 240 |
| 246 | 241 |
| 247 class WebSocketInfo { | 242 class WebSocketInfo { |
| 248 int messageCount = 0; | 243 int messageCount = 0; |
| 249 } | 244 } |
| 250 | 245 |
| 251 | 246 |
| 252 Future testHashCode(int totalConnections) { | 247 void testHashCode(int totalConnections) { |
| 253 Completer done = new Completer(); | 248 ReceivePort keepAlive = new ReceivePort(); |
| 254 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 249 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 255 HttpClient client = new HttpClient(); | 250 HttpClient client = new HttpClient(); |
| 256 Map connections = new Map(); | 251 Map connections = new Map(); |
| 257 | 252 |
| 258 server.listen(SERVER_ADDRESS, | 253 server.listen(SERVER_ADDRESS, |
| 259 0, | 254 0, |
| 260 backlog: totalConnections, | 255 backlog: totalConnections, |
| 261 certificate_name: "CN=$HOST_NAME"); | 256 certificate_name: "CN=$HOST_NAME"); |
| 262 | 257 |
| 263 void handleMessage(conn, message) { | 258 void handleMessage(conn, message) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 281 conn.onMessage = (Object message) { | 276 conn.onMessage = (Object message) { |
| 282 handleMessage(conn, message); | 277 handleMessage(conn, message); |
| 283 }; | 278 }; |
| 284 conn.onClosed = (status, reason) { | 279 conn.onClosed = (status, reason) { |
| 285 closeCount++; | 280 closeCount++; |
| 286 var info = connections[conn]; | 281 var info = connections[conn]; |
| 287 Expect.equals(10, info.messageCount); | 282 Expect.equals(10, info.messageCount); |
| 288 if (closeCount == totalConnections) { | 283 if (closeCount == totalConnections) { |
| 289 client.shutdown(); | 284 client.shutdown(); |
| 290 server.close(); | 285 server.close(); |
| 291 done.complete(); | 286 keepAlive.close(); |
| 292 } | 287 } |
| 293 }; | 288 }; |
| 294 conn.send(messageText); | 289 conn.send(messageText); |
| 295 }; | 290 }; |
| 296 server.defaultRequestHandler = wsHandler.onRequest; | 291 server.defaultRequestHandler = wsHandler.onRequest; |
| 297 | 292 |
| 298 for (int i = 0; i < totalConnections; i++) { | 293 for (int i = 0; i < totalConnections; i++) { |
| 299 HttpClientConnection conn = client.getUrl(new Uri.fromString( | 294 HttpClientConnection conn = client.getUrl(new Uri.fromString( |
| 300 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | 295 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); |
| 301 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 296 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 302 wsconn.onMessage = (message) => wsconn.send(message); | 297 wsconn.onMessage = (message) => wsconn.send(message); |
| 303 } | 298 } |
| 304 return done.future; | |
| 305 } | 299 } |
| 306 | 300 |
| 307 | 301 |
| 308 Future testW3CInterface( | 302 void testW3CInterface( |
| 309 int totalConnections, int closeStatus, String closeReason) { | 303 int totalConnections, int closeStatus, String closeReason) { |
| 304 ReceivePort keepAlive = new ReceivePort(); | |
| 310 List<Future> tasks = []; | 305 List<Future> tasks = []; |
| 311 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 306 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 312 | 307 |
| 313 server.listen(SERVER_ADDRESS, | 308 server.listen(SERVER_ADDRESS, |
| 314 0, | 309 0, |
| 315 backlog: totalConnections, | 310 backlog: totalConnections, |
| 316 certificate_name: "CN=$HOST_NAME"); | 311 certificate_name: "CN=$HOST_NAME"); |
| 317 | 312 |
| 318 // Create a web socket handler and set is as the HTTP server default | 313 // Create a web socket handler and set is as the HTTP server default |
| 319 // handler. | 314 // handler. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 Expect.equals(3002, event.code); | 374 Expect.equals(3002, event.code); |
| 380 Expect.equals("Got tired", event.reason); | 375 Expect.equals("Got tired", event.reason); |
| 381 Expect.equals(WebSocket.CLOSED, websocket.readyState); | 376 Expect.equals(WebSocket.CLOSED, websocket.readyState); |
| 382 clientDone.complete(null); | 377 clientDone.complete(null); |
| 383 }; | 378 }; |
| 384 } | 379 } |
| 385 | 380 |
| 386 for (int i = 0; i < totalConnections; i++) { | 381 for (int i = 0; i < totalConnections; i++) { |
| 387 webSocketConnection(); | 382 webSocketConnection(); |
| 388 } | 383 } |
| 389 return Future.wait(tasks); | 384 Future.wait(tasks).then((_) { |
| 385 keepAlive.close(); | |
| 386 }); | |
| 390 } | 387 } |
| 391 | 388 |
| 392 | 389 |
| 393 void InitializeSSL() { | 390 void InitializeSSL() { |
| 394 var testPkcertDatabase = | 391 var testPkcertDatabase = |
| 395 new Path(new Options().script).directoryPath.append("pkcert/"); | 392 new Path(new Options().script).directoryPath.append("pkcert/"); |
| 396 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | 393 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), |
| 397 password: "dartdart"); | 394 password: "dartdart"); |
| 398 } | 395 } |
| 399 | 396 |
| 400 | 397 |
| 401 Future runTests() => | 398 /** |
| 402 testRequestResponseClientCloses(2, null, null).then((_) => | 399 * Run all the tests in parallel, complete when the last one finishes. |
| 403 testRequestResponseClientCloses(2, 3001, null)).then((_) => | 400 */ |
| 404 testRequestResponseClientCloses(2, 3002, "Got tired")).then((_) => | 401 runTests() { |
| 405 testRequestResponseServerCloses(2, null, null)).then((_) => | 402 testRequestResponseClientCloses(2, null, null); |
| 406 testRequestResponseServerCloses(2, 3001, null)).then((_) => | 403 testRequestResponseClientCloses(2, 3001, null); |
| 407 testRequestResponseServerCloses(2, 3002, "Got tired")).then((_) => | 404 testRequestResponseClientCloses(2, 3002, "Got tired"); |
| 408 testMessageLength(125)).then((_) => | 405 testRequestResponseServerCloses(2, null, null); |
| 409 testMessageLength(126)).then((_) => | 406 testRequestResponseServerCloses(2, 3001, null); |
| 410 testMessageLength(127)).then((_) => | 407 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 411 testMessageLength(65535)).then((_) => | 408 testMessageLength(125); |
| 412 testMessageLength(65536)).then((_) => | 409 testMessageLength(126); |
| 413 testNoUpgrade()).then((_) => | 410 testMessageLength(127); |
| 414 testUsePOST()).then((_) => | 411 testMessageLength(65535); |
| 415 testHashCode(2)).then((_) => | 412 testMessageLength(65536); |
| 416 testW3CInterface(2, 3002, "Got tired")); | 413 testNoUpgrade(); |
| 414 testUsePOST(); | |
| 415 testHashCode(2); | |
| 416 testW3CInterface(2, 3002, "Got tired"); | |
| 417 } | |
| 417 | 418 |
| 418 | 419 |
| 419 main() { | 420 main() { |
| 420 ReceivePort keepAlive = new ReceivePort(); | 421 runTests(); |
| 421 runTests().then((_) { | 422 InitializeSSL(); |
|
Anders Johnsen
2013/01/17 14:17:33
initializeSSL
| |
| 422 InitializeSSL(); | 423 secure = true; |
| 423 secure = true; | 424 runTests(); |
|
Anders Johnsen
2013/01/17 14:17:33
Put in secure == null below, so we can check that
| |
| 424 }).then((_) => | |
| 425 runTests()).then((_) { | |
| 426 keepAlive.close(); | |
| 427 }); | |
| 428 } | 425 } |
| OLD | NEW |