| 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 // This test tests TLS session resume, by making multiple client connections | 5 // This test tests TLS session resume, by making multiple client connections |
| 6 // on the same port to the same server, with a delay of 200 ms between them. | 6 // on the same port to the same server, with a delay of 200 ms between them. |
| 7 // The unmodified secure_server_test creates all sessions simultaneously, | 7 // The unmodified secure_server_test creates all sessions simultaneously, |
| 8 // which means that no handshake completes and caches its keys in the session | 8 // which means that no handshake completes and caches its keys in the session |
| 9 // cache in time for other connections to use it. | 9 // cache in time for other connections to use it. |
| 10 // | 10 // |
| 11 // Session resume is currently disabled - see issue | 11 // Session resume is currently disabled - see issue |
| 12 // https://code.google.com/p/dart/issues/detail?id=7230 | 12 // https://code.google.com/p/dart/issues/detail?id=7230 |
| 13 // | 13 // |
| 14 // VMOptions= | 14 // VMOptions= |
| 15 // VMOptions=--short_socket_read | 15 // VMOptions=--short_socket_read |
| 16 // VMOptions=--short_socket_write | 16 // VMOptions=--short_socket_write |
| 17 // VMOptions=--short_socket_read --short_socket_write | 17 // VMOptions=--short_socket_read --short_socket_write |
| 18 | 18 |
| 19 import "dart:async"; | 19 import "dart:async"; |
| 20 import "dart:io"; | 20 import "dart:io"; |
| 21 import "dart:isolate"; | 21 import "dart:isolate"; |
| 22 | 22 |
| 23 import "package:expect/expect.dart"; | 23 import "package:expect/expect.dart"; |
| 24 import "package:async_helper/async_helper.dart"; | 24 import "package:async_helper/async_helper.dart"; |
| 25 | 25 |
| 26 InternetAddress HOST; | 26 InternetAddress HOST; |
| 27 const CERTIFICATE = "localhost_cert"; | 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 Future<SecureServerSocket> startServer() { | 38 Future<SecureServerSocket> startServer() { |
| 29 return SecureServerSocket.bind(HOST, | 39 return SecureServerSocket.bind(HOST, |
| 30 0, | 40 0, |
| 31 CERTIFICATE).then((server) { | 41 serverContext).then((server) { |
| 32 server.listen((SecureSocket client) { | 42 server.listen((SecureSocket client) { |
| 33 client.fold(<int>[], (message, data) => message..addAll(data)) | 43 client.fold(<int>[], (message, data) => message..addAll(data)) |
| 34 .then((message) { | 44 .then((message) { |
| 35 String received = new String.fromCharCodes(message); | 45 String received = new String.fromCharCodes(message); |
| 36 Expect.isTrue(received.contains("Hello from client ")); | 46 Expect.isTrue(received.contains("Hello from client ")); |
| 37 String name = received.substring(received.indexOf("client ") + 7); | 47 String name = received.substring(received.indexOf("client ") + 7); |
| 38 client.write("Welcome, client $name"); | 48 client.write("Welcome, client $name"); |
| 39 client.close(); | 49 client.close(); |
| 40 }); | 50 }); |
| 41 }); | 51 }); |
| 42 return server; | 52 return server; |
| 43 }); | 53 }); |
| 44 } | 54 } |
| 45 | 55 |
| 46 Future testClient(server, name) { | 56 Future testClient(server, name) { |
| 47 return SecureSocket.connect(HOST, server.port).then((socket) { | 57 return SecureSocket.connect(HOST, server.port, context: clientContext) |
| 58 .then((socket) { |
| 48 socket.write("Hello from client $name"); | 59 socket.write("Hello from client $name"); |
| 49 socket.close(); | 60 socket.close(); |
| 50 return socket.fold(<int>[], (message, data) => message..addAll(data)) | 61 return socket.fold(<int>[], (message, data) => message..addAll(data)) |
| 51 .then((message) { | 62 .then((message) { |
| 52 Expect.listEquals("Welcome, client $name".codeUnits, message); | 63 Expect.listEquals("Welcome, client $name".codeUnits, message); |
| 53 return server; | 64 return server; |
| 54 }); | 65 }); |
| 55 }); | 66 }); |
| 56 } | 67 } |
| 57 | 68 |
| 58 void main() { | 69 void main() { |
| 59 asyncStart(); | 70 asyncStart(); |
| 60 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
| 61 SecureSocket.initialize(database: certificateDatabase, | |
| 62 password: 'dartdart'); | |
| 63 InternetAddress.lookup("localhost").then((hosts) { | 71 InternetAddress.lookup("localhost").then((hosts) { |
| 64 HOST = hosts.first; | 72 HOST = hosts.first; |
| 65 runTests().then((_) => asyncEnd()); | 73 runTests().then((_) => asyncEnd()); |
| 66 }); | 74 }); |
| 67 } | 75 } |
| 68 | 76 |
| 69 Future runTests() { | 77 Future runTests() { |
| 70 Duration delay = const Duration(milliseconds: 0); | 78 Duration delay = const Duration(milliseconds: 0); |
| 71 Duration delay_between_connections = const Duration(milliseconds: 300); | 79 Duration delay_between_connections = const Duration(milliseconds: 300); |
| 72 return startServer() | 80 return startServer() |
| 73 .then((server) => Future.wait( | 81 .then((server) => Future.wait( |
| 74 ['able', 'baker', 'charlie', 'dozen', 'elapse'] | 82 ['able', 'baker', 'charlie', 'dozen', 'elapse'] |
| 75 .map((name) { | 83 .map((name) { |
| 76 delay += delay_between_connections; | 84 delay += delay_between_connections; |
| 77 return new Future.delayed(delay, () => server) | 85 return new Future.delayed(delay, () => server) |
| 78 .then((server) => testClient(server, name)); | 86 .then((server) => testClient(server, name)); |
| 79 }))) | 87 }))) |
| 80 .then((servers) => servers.first.close()); | 88 .then((servers) => servers.first.close()); |
| 81 } | 89 } |
| OLD | NEW |