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 // OtherResources=certificates/server_chain.pem | 9 // OtherResources=certificates/server_chain.pem |
10 // OtherResources=certificates/server_key.pem | 10 // OtherResources=certificates/server_key.pem |
11 // OtherResources=certificates/trusted_certs.pem | 11 // OtherResources=certificates/trusted_certs.pem |
12 | 12 |
13 import "dart:async"; | 13 import "dart:async"; |
14 import "dart:io"; | 14 import "dart:io"; |
15 | 15 |
16 import "package:async_helper/async_helper.dart"; | 16 import "package:async_helper/async_helper.dart"; |
17 import "package:expect/expect.dart"; | 17 import "package:expect/expect.dart"; |
18 | 18 |
19 InternetAddress HOST; | 19 InternetAddress HOST; |
20 SecureServerSocket SERVER; | 20 SecureServerSocket SERVER; |
21 | 21 |
22 String localFile(path) => Platform.script.resolve(path).toFilePath(); | 22 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
23 | 23 |
24 SecurityContext serverContext = new SecurityContext() | 24 SecurityContext serverContext = new SecurityContext() |
25 ..useCertificateChain(localFile('certificates/server_chain.pem')) | 25 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
26 ..usePrivateKey(localFile('certificates/server_key.pem'), | 26 ..usePrivateKey(localFile('certificates/server_key.pem'), |
27 password: 'dartdart'); | 27 password: 'dartdart'); |
28 | 28 |
29 SecurityContext clientContext = new SecurityContext() | 29 SecurityContext clientContext = new SecurityContext() |
30 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 30 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
31 | 31 |
32 Future startServer() { | 32 Future startServer() { |
33 return SecureServerSocket.bind(HOST, 0, serverContext).then((server) { | 33 return SecureServerSocket.bind(HOST, 0, serverContext).then((server) { |
34 SERVER = server; | 34 SERVER = server; |
35 SERVER.listen((SecureSocket client) { | 35 SERVER.listen((SecureSocket client) { |
36 client.fold(<int>[], (message, data) => message..addAll(data)) | 36 client.fold(<int>[], (message, data) => message..addAll(data)).then( |
37 .then((message) { | 37 (message) { |
38 String received = new String.fromCharCodes(message); | 38 String received = new String.fromCharCodes(message); |
39 Expect.isTrue(received.contains("Hello from client ")); | 39 Expect.isTrue(received.contains("Hello from client ")); |
40 String name = received.substring(received.indexOf("client ") + 7); | 40 String name = received.substring(received.indexOf("client ") + 7); |
41 client.add("Welcome, client $name".codeUnits); | 41 client.add("Welcome, client $name".codeUnits); |
42 client.close(); | 42 client.close(); |
43 }); | 43 }); |
44 }); | 44 }); |
45 }); | 45 }); |
46 } | 46 } |
47 | 47 |
48 Future testClient(name) { | 48 Future testClient(name) { |
49 return SecureSocket.connect(HOST, SERVER.port, context: clientContext) | 49 return SecureSocket |
50 .then((socket) { | 50 .connect(HOST, SERVER.port, context: clientContext) |
| 51 .then((socket) { |
51 socket.add("Hello from client $name".codeUnits); | 52 socket.add("Hello from client $name".codeUnits); |
52 socket.close(); | 53 socket.close(); |
53 return socket.fold(<int>[], (message, data) => message..addAll(data)) | 54 return socket.fold(<int>[], (message, data) => message..addAll(data)).then( |
54 .then((message) { | 55 (message) { |
55 Expect.listEquals("Welcome, client $name".codeUnits, message); | 56 Expect.listEquals("Welcome, client $name".codeUnits, message); |
56 }); | 57 }); |
57 }); | 58 }); |
58 } | 59 } |
59 | 60 |
60 void main() { | 61 void main() { |
61 asyncStart(); | 62 asyncStart(); |
62 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) | 63 InternetAddress |
| 64 .lookup("localhost") |
| 65 .then((hosts) => HOST = hosts.first) |
63 .then((_) => startServer()) | 66 .then((_) => startServer()) |
64 .then((_) => ['ale', 'bar', 'che', 'den', 'els'].map(testClient)) | 67 .then((_) => ['ale', 'bar', 'che', 'den', 'els'].map(testClient)) |
65 .then((futures) => Future.wait(futures)) | 68 .then((futures) => Future.wait(futures)) |
66 .then((_) => SERVER.close()) | 69 .then((_) => SERVER.close()) |
67 .then((_) => asyncEnd()); | 70 .then((_) => asyncEnd()); |
68 } | 71 } |
OLD | NEW |