| 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 library dart.io; | 10 library dart.io; |
| 11 | 11 |
| 12 import "dart:async"; | 12 import "dart:async"; |
| 13 import "dart:io"; | 13 import "dart:io"; |
| 14 import "dart:math"; | 14 import "dart:math"; |
| 15 import "dart:typed_data"; | 15 import "dart:typed_data"; |
| 16 | 16 |
| 17 import "package:async_helper/async_helper.dart"; | 17 import "package:async_helper/async_helper.dart"; |
| 18 import "package:expect/expect.dart"; | 18 import "package:expect/expect.dart"; |
| 19 import "package:path/path.dart"; | 19 import "package:path/path.dart"; |
| 20 | 20 |
| 21 part "../../../sdk/lib/io/crypto.dart"; | 21 part "../../../sdk/lib/io/crypto.dart"; |
| 22 | 22 |
| 23 | 23 |
| 24 const String webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 24 const String webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 25 const String CERT_NAME = 'localhost_cert'; | 25 const String CERT_NAME = 'localhost_cert'; |
| 26 const String HOST_NAME = 'localhost'; | 26 const String HOST_NAME = 'localhost'; |
| 27 | 27 |
| 28 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 29 |
| 30 SecurityContext serverContext = new SecurityContext() |
| 31 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 32 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 33 password: 'dartdart'); |
| 34 |
| 35 SecurityContext clientContext = new SecurityContext() |
| 36 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 37 |
| 28 /** | 38 /** |
| 29 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. | 39 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. |
| 30 */ | 40 */ |
| 31 class SecurityConfiguration { | 41 class SecurityConfiguration { |
| 32 final bool secure; | 42 final bool secure; |
| 33 | 43 |
| 34 SecurityConfiguration({bool this.secure}); | 44 SecurityConfiguration({bool this.secure}); |
| 35 | 45 |
| 36 Future<HttpServer> createServer({int backlog: 0}) => | 46 Future<HttpServer> createServer({int backlog: 0}) => |
| 37 secure ? HttpServer.bindSecure(HOST_NAME, | 47 secure ? HttpServer.bindSecure(HOST_NAME, |
| 38 0, | 48 0, |
| 39 backlog: backlog, | 49 serverContext, |
| 40 certificateName: CERT_NAME) | 50 backlog: backlog) |
| 41 : HttpServer.bind(HOST_NAME, | 51 : HttpServer.bind(HOST_NAME, |
| 42 0, | 52 0, |
| 43 backlog: backlog); | 53 backlog: backlog); |
| 44 | 54 |
| 45 Future<WebSocket> createClient(int port) => | 55 Future<WebSocket> createClient(int port) => |
| 56 // TODO(whesse): Add a client context argument to WebSocket.connect. |
| 46 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); | 57 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); |
| 47 | 58 |
| 48 | 59 |
| 49 void testForceCloseServerEnd(int totalConnections) { | 60 void testForceCloseServerEnd(int totalConnections) { |
| 50 createServer().then((server) { | 61 createServer().then((server) { |
| 51 server.listen((request) { | 62 server.listen((request) { |
| 52 var response = request.response; | 63 var response = request.response; |
| 53 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; | 64 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; |
| 54 response.headers.set(HttpHeaders.CONNECTION, "upgrade"); | 65 response.headers.set(HttpHeaders.CONNECTION, "upgrade"); |
| 55 response.headers.set(HttpHeaders.UPGRADE, "websocket"); | 66 response.headers.set(HttpHeaders.UPGRADE, "websocket"); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 82 } | 93 } |
| 83 }); | 94 }); |
| 84 } | 95 } |
| 85 | 96 |
| 86 void runTests() { | 97 void runTests() { |
| 87 testForceCloseServerEnd(10); | 98 testForceCloseServerEnd(10); |
| 88 } | 99 } |
| 89 } | 100 } |
| 90 | 101 |
| 91 | 102 |
| 92 void initializeSSL() { | |
| 93 var testPkcertDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
| 94 SecureSocket.initialize(database: testPkcertDatabase, | |
| 95 password: "dartdart"); | |
| 96 } | |
| 97 | |
| 98 | |
| 99 main() { | 103 main() { |
| 100 asyncStart(); | 104 asyncStart(); |
| 101 new SecurityConfiguration(secure: false).runTests(); | 105 new SecurityConfiguration(secure: false).runTests(); |
| 102 initializeSSL(); | 106 // TODO(whesse): WebSocket.connect needs an optional context: parameter |
| 103 new SecurityConfiguration(secure: true).runTests(); | 107 // new SecurityConfiguration(secure: true).runTests(); |
| 104 asyncEnd(); | 108 asyncEnd(); |
| 105 } | 109 } |
| 106 | 110 |
| OLD | NEW |