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 "dart:async"; | 10 import "dart:async"; |
11 import "dart:io"; | 11 import "dart:io"; |
12 | 12 |
13 import "package:async_helper/async_helper.dart"; | 13 import "package:async_helper/async_helper.dart"; |
14 import "package:expect/expect.dart"; | 14 import "package:expect/expect.dart"; |
15 | 15 |
16 InternetAddress HOST; | 16 InternetAddress HOST; |
17 const CERTIFICATE = "localhost_cert"; | 17 |
| 18 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 19 |
| 20 SecurityContext serverContext = new SecurityContext() |
| 21 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 22 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 23 password: 'dartdart'); |
| 24 |
| 25 SecurityContext clientContext = new SecurityContext() |
| 26 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 27 |
| 28 |
18 Future<SecureServerSocket> startEchoServer() { | 29 Future<SecureServerSocket> startEchoServer() { |
19 return SecureServerSocket.bind(HOST, | 30 return SecureServerSocket.bind(HOST, |
20 0, | 31 0, |
21 CERTIFICATE).then((server) { | 32 serverContext).then((server) { |
22 server.listen((SecureSocket client) { | 33 server.listen((SecureSocket client) { |
23 client.fold(<int>[], (message, data) => message..addAll(data)) | 34 client.fold(<int>[], (message, data) => message..addAll(data)) |
24 .then((message) { | 35 .then((message) { |
25 client.add(message); | 36 client.add(message); |
26 client.close(); | 37 client.close(); |
27 }); | 38 }); |
28 }); | 39 }); |
29 return server; | 40 return server; |
30 }); | 41 }); |
31 } | 42 } |
32 | 43 |
33 Future testClient(server) { | 44 Future testClient(server) { |
34 return SecureSocket.connect(HOST, server.port).then((socket) { | 45 return SecureSocket.connect(HOST, server.port, context: clientContext) |
| 46 .then((socket) { |
35 socket.write("Hello server."); | 47 socket.write("Hello server."); |
36 socket.close(); | 48 socket.close(); |
37 return socket.fold(<int>[], (message, data) => message..addAll(data)) | 49 return socket.fold(<int>[], (message, data) => message..addAll(data)) |
38 .then((message) { | 50 .then((message) { |
39 Expect.listEquals("Hello server.".codeUnits, message); | 51 Expect.listEquals("Hello server.".codeUnits, message); |
40 return server; | 52 return server; |
41 }); | 53 }); |
42 }); | 54 }); |
43 } | 55 } |
44 | 56 |
45 void main() { | 57 void main() { |
46 asyncStart(); | 58 asyncStart(); |
47 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
48 SecureSocket.initialize(database: certificateDatabase, | |
49 password: 'dartdart'); | |
50 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first ) | 59 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first ) |
51 .then((_) => startEchoServer()) | 60 .then((_) => startEchoServer()) |
52 .then(testClient) | 61 .then(testClient) |
53 .then((server) => server.close()) | 62 .then((server) => server.close()) |
54 .then((_) => asyncEnd()); | 63 .then((_) => asyncEnd()); |
55 } | 64 } |
OLD | NEW |