| 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 "package:expect/expect.dart"; | |
| 11 import "package:path/path.dart"; | |
| 12 import "dart:async"; | 10 import "dart:async"; |
| 13 import "dart:io"; | 11 import "dart:io"; |
| 14 import "dart:isolate"; | |
| 15 | 12 |
| 16 const HOST_NAME = "localhost"; | 13 import "package:async_helper/async_helper.dart"; |
| 14 import "package:expect/expect.dart"; |
| 15 |
| 16 InternetAddress HOST; |
| 17 SecureServerSocket SERVER; |
| 17 const CERTIFICATE = "localhost_cert"; | 18 const CERTIFICATE = "localhost_cert"; |
| 18 Future<SecureServerSocket> startServer() { | 19 |
| 19 return SecureServerSocket.bind(HOST_NAME, | 20 Future startServer() { |
| 20 0, | 21 return SecureServerSocket.bind(HOST, 0, CERTIFICATE).then((server) { |
| 21 CERTIFICATE).then((server) { | 22 SERVER = server; |
| 22 server.listen((SecureSocket client) { | 23 SERVER.listen((SecureSocket client) { |
| 23 client.fold(<int>[], (message, data) => message..addAll(data)) | 24 client.fold(<int>[], (message, data) => message..addAll(data)) |
| 24 .then((message) { | 25 .then((message) { |
| 25 String received = new String.fromCharCodes(message); | 26 String received = new String.fromCharCodes(message); |
| 26 Expect.isTrue(received.contains("Hello from client ")); | 27 Expect.isTrue(received.contains("Hello from client ")); |
| 27 String name = received.substring(received.indexOf("client ") + 7); | 28 String name = received.substring(received.indexOf("client ") + 7); |
| 28 client.add("Welcome, client $name".codeUnits); | 29 client.add("Welcome, client $name".codeUnits); |
| 29 client.close(); | 30 client.close(); |
| 30 }); | 31 }); |
| 31 }); | 32 }); |
| 32 return server; | |
| 33 }); | 33 }); |
| 34 } | 34 } |
| 35 | 35 |
| 36 Future testClient(server, name) { | 36 Future testClient(name) { |
| 37 return SecureSocket.connect(HOST_NAME, server.port).then((socket) { | 37 return SecureSocket.connect(HOST, SERVER.port).then((socket) { |
| 38 socket.add("Hello from client $name".codeUnits); | 38 socket.add("Hello from client $name".codeUnits); |
| 39 socket.close(); | 39 socket.close(); |
| 40 return socket.fold(<int>[], (message, data) => message..addAll(data)) | 40 return socket.fold(<int>[], (message, data) => message..addAll(data)) |
| 41 .then((message) { | 41 .then((message) { |
| 42 Expect.listEquals("Welcome, client $name".codeUnits, message); | 42 Expect.listEquals("Welcome, client $name".codeUnits, message); |
| 43 return server; | |
| 44 }); | 43 }); |
| 45 }); | 44 }); |
| 46 } | 45 } |
| 47 | 46 |
| 48 void main() { | 47 void main() { |
| 48 asyncStart(); |
| 49 var certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | 49 var certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); |
| 50 SecureSocket.initialize(database: certificateDatabase, | 50 SecureSocket.initialize(database: certificateDatabase, |
| 51 password: 'dartdart'); | 51 password: 'dartdart'); |
| 52 | 52 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) |
| 53 startServer() | 53 .then((_) => startServer()) |
| 54 .then((server) => Future.wait( | 54 .then((_) => ['ale', 'bar', 'che', 'den', 'els'].map(testClient)) |
| 55 ['able', 'baker', 'charlie', 'dozen', 'elapse'] | 55 .then((futures) => Future.wait(futures)) |
| 56 .map((name) => testClient(server, name)))) | 56 .then((_) => SERVER.close()) |
| 57 .then((servers) => servers.first.close()); | 57 .then((_) => asyncEnd()); |
| 58 } | 58 } |
| OLD | NEW |