OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 library dart.io; |
| 11 |
| 12 import "dart:async"; |
| 13 import "dart:io"; |
| 14 import "dart:typed_data"; |
| 15 |
| 16 import "package:async_helper/async_helper.dart"; |
| 17 import "package:expect/expect.dart"; |
| 18 import "package:path/path.dart"; |
| 19 |
| 20 part "../../../sdk/lib/io/crypto.dart"; |
| 21 |
| 22 |
| 23 const String webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| 24 const String CERT_NAME = 'localhost_cert'; |
| 25 const String HOST_NAME = 'localhost'; |
| 26 |
| 27 /** |
| 28 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. |
| 29 */ |
| 30 class SecurityConfiguration { |
| 31 final bool secure; |
| 32 |
| 33 SecurityConfiguration({bool this.secure}); |
| 34 |
| 35 Future<HttpServer> createServer({int backlog: 0}) => |
| 36 secure ? HttpServer.bindSecure(HOST_NAME, |
| 37 0, |
| 38 backlog: backlog, |
| 39 certificateName: CERT_NAME) |
| 40 : HttpServer.bind(HOST_NAME, |
| 41 0, |
| 42 backlog: backlog); |
| 43 |
| 44 Future<WebSocket> createClient(int port) => |
| 45 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); |
| 46 |
| 47 |
| 48 void testForceCloseServerEnd(int totalConnections) { |
| 49 createServer().then((server) { |
| 50 server.listen((request) { |
| 51 var response = request.response; |
| 52 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; |
| 53 response.headers.set(HttpHeaders.CONNECTION, "upgrade"); |
| 54 response.headers.set(HttpHeaders.UPGRADE, "websocket"); |
| 55 String key = request.headers.value("Sec-WebSocket-Key"); |
| 56 _SHA1 sha1 = new _SHA1(); |
| 57 sha1.add("$key$webSocketGUID".codeUnits); |
| 58 String accept = _CryptoUtils.bytesToBase64(sha1.close()); |
| 59 response.headers.add("Sec-WebSocket-Accept", accept); |
| 60 response.headers.contentLength = 0; |
| 61 response.detachSocket().then((socket) { |
| 62 socket.destroy(); |
| 63 }); |
| 64 }); |
| 65 |
| 66 int closeCount = 0; |
| 67 for (int i = 0; i < totalConnections; i++) { |
| 68 createClient(server.port).then((webSocket) { |
| 69 webSocket.add("Hello, world!"); |
| 70 webSocket.listen( |
| 71 (message) { |
| 72 Expect.fail("unexpected message"); |
| 73 }, |
| 74 onDone: () { |
| 75 closeCount++; |
| 76 if (closeCount == totalConnections) { |
| 77 server.close(); |
| 78 } |
| 79 }); |
| 80 }); |
| 81 } |
| 82 }); |
| 83 } |
| 84 |
| 85 void runTests() { |
| 86 testForceCloseServerEnd(10); |
| 87 } |
| 88 } |
| 89 |
| 90 |
| 91 void initializeSSL() { |
| 92 var testPkcertDatabase = join(dirname(Platform.script), 'pkcert'); |
| 93 SecureSocket.initialize(database: testPkcertDatabase, |
| 94 password: "dartdart"); |
| 95 } |
| 96 |
| 97 |
| 98 main() { |
| 99 asyncStart(); |
| 100 new SecurityConfiguration(secure: false).runTests(); |
| 101 initializeSSL(); |
| 102 new SecurityConfiguration(secure: true).runTests(); |
| 103 asyncEnd(); |
| 104 } |
| 105 |
OLD | NEW |