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 import "dart:async"; | 10 import "dart:async"; |
11 import "dart:io"; | 11 import "dart:io"; |
12 import "dart:typed_data"; | 12 import "dart:typed_data"; |
13 | 13 |
14 import "package:async_helper/async_helper.dart"; | 14 import "package:async_helper/async_helper.dart"; |
| 15 import "package:crypto/crypto.dart"; |
15 import "package:expect/expect.dart"; | 16 import "package:expect/expect.dart"; |
16 import "package:path/path.dart"; | 17 import "package:path/path.dart"; |
17 | 18 |
| 19 const WEB_SOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 20 |
18 const String CERT_NAME = 'localhost_cert'; | 21 const String CERT_NAME = 'localhost_cert'; |
19 const String HOST_NAME = 'localhost'; | 22 const String HOST_NAME = 'localhost'; |
20 | 23 |
21 /** | 24 /** |
22 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. | 25 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. |
23 */ | 26 */ |
24 class SecurityConfiguration { | 27 class SecurityConfiguration { |
25 final bool secure; | 28 final bool secure; |
26 | 29 |
27 SecurityConfiguration({bool this.secure}); | 30 SecurityConfiguration({bool this.secure}); |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 })); | 379 })); |
377 } | 380 } |
378 | 381 |
379 Future.wait(futures).then((_) { | 382 Future.wait(futures).then((_) { |
380 server.close(); | 383 server.close(); |
381 client.close(); | 384 client.close(); |
382 }); | 385 }); |
383 }); | 386 }); |
384 } | 387 } |
385 | 388 |
| 389 testFromSocket() { |
| 390 createServer().then((server) { |
| 391 server.listen((request) { |
| 392 Expect.equals('Upgrade', request.headers.value(HttpHeaders.CONNECTION)); |
| 393 Expect.equals('websocket', request.headers.value(HttpHeaders.UPGRADE)); |
| 394 |
| 395 var key = request.headers.value('Sec-WebSocket-Key'); |
| 396 var sha1 = new SHA1()..add("$key$WEB_SOCKET_GUID".codeUnits); |
| 397 var accept = CryptoUtils.bytesToBase64(sha1.close()); |
| 398 request.response |
| 399 ..statusCode = HttpStatus.SWITCHING_PROTOCOLS |
| 400 ..headers.add(HttpHeaders.CONNECTION, "Upgrade") |
| 401 ..headers.add(HttpHeaders.UPGRADE, "websocket") |
| 402 ..headers.add("Sec-WebSocket-Accept", accept); |
| 403 request.response.contentLength = 0; |
| 404 return request.response.detachSocket() |
| 405 .then((socket) => new WebSocket.fromSocket(socket)) |
| 406 .then((websocket) { |
| 407 websocket.add("Hello"); |
| 408 websocket.close(); |
| 409 }); |
| 410 }); |
| 411 |
| 412 var url = '${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}/'; |
| 413 |
| 414 var client = new HttpClient(); |
| 415 var completer = new Completer(); |
| 416 WebSocket.connect(url).then((websocket) { |
| 417 return websocket.listen((message) { |
| 418 Expect.equals("Hello", message); |
| 419 websocket.close(); |
| 420 }).asFuture(); |
| 421 }).then((_) => server.close()); |
| 422 }); |
| 423 } |
| 424 |
386 void runTests() { | 425 void runTests() { |
387 testRequestResponseClientCloses(2, null, null); | 426 testRequestResponseClientCloses(2, null, null); |
388 testRequestResponseClientCloses(2, 3001, null); | 427 testRequestResponseClientCloses(2, 3001, null); |
389 testRequestResponseClientCloses(2, 3002, "Got tired"); | 428 testRequestResponseClientCloses(2, 3002, "Got tired"); |
390 testRequestResponseServerCloses(2, null, null); | 429 testRequestResponseServerCloses(2, null, null); |
391 testRequestResponseServerCloses(2, 3001, null); | 430 testRequestResponseServerCloses(2, 3001, null); |
392 testRequestResponseServerCloses(2, 3002, "Got tired"); | 431 testRequestResponseServerCloses(2, 3002, "Got tired"); |
393 testMessageLength(125); | 432 testMessageLength(125); |
394 testMessageLength(126); | 433 testMessageLength(126); |
395 testMessageLength(127); | 434 testMessageLength(127); |
396 testMessageLength(65535); | 435 testMessageLength(65535); |
397 testMessageLength(65536); | 436 testMessageLength(65536); |
398 testDoubleCloseClient(); | 437 testDoubleCloseClient(); |
399 testDoubleCloseServer(); | 438 testDoubleCloseServer(); |
400 testImmediateCloseServer(); | 439 testImmediateCloseServer(); |
401 testImmediateCloseClient(); | 440 testImmediateCloseClient(); |
402 testNoUpgrade(); | 441 testNoUpgrade(); |
403 testUsePOST(); | 442 testUsePOST(); |
404 testConnections(10, 3002, "Got tired"); | 443 testConnections(10, 3002, "Got tired"); |
405 testIndividualUpgrade(5); | 444 testIndividualUpgrade(5); |
| 445 testFromSocket(); |
406 } | 446 } |
407 } | 447 } |
408 | 448 |
409 | 449 |
410 void initializeSSL() { | 450 void initializeSSL() { |
411 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); | 451 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); |
412 SecureSocket.initialize(database: testPkcertDatabase, | 452 SecureSocket.initialize(database: testPkcertDatabase, |
413 password: "dartdart"); | 453 password: "dartdart"); |
414 } | 454 } |
415 | 455 |
416 | 456 |
417 main() { | 457 main() { |
418 asyncStart(); | 458 asyncStart(); |
419 new SecurityConfiguration(secure: false).runTests(); | 459 new SecurityConfiguration(secure: false).runTests(); |
420 initializeSSL(); | 460 initializeSSL(); |
421 new SecurityConfiguration(secure: true).runTests(); | 461 new SecurityConfiguration(secure: true).runTests(); |
422 asyncEnd(); | 462 asyncEnd(); |
423 } | 463 } |
OLD | NEW |