| 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 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 17 |
| 18 SecurityContext serverContext = new SecurityContext() |
| 19 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 20 ..usePrivateKey(localFile('certificates/server_key.pem'), |
| 21 password: 'dartdart'); |
| 22 |
| 23 SecurityContext clientContext = new SecurityContext() |
| 24 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 25 |
| 16 InternetAddress HOST; | 26 InternetAddress HOST; |
| 17 const CERTIFICATE = "localhost_cert"; | |
| 18 Future<RawSecureServerSocket> startEchoServer() { | 27 Future<RawSecureServerSocket> startEchoServer() { |
| 19 return RawSecureServerSocket.bind(HOST, | 28 return RawSecureServerSocket.bind(HOST, |
| 20 0, | 29 0, |
| 21 CERTIFICATE).then((server) { | 30 serverContext).then((server) { |
| 22 server.listen((RawSecureSocket client) { | 31 server.listen((RawSecureSocket client) { |
| 23 List<List<int>> readChunks = <List<int>>[]; | 32 List<List<int>> readChunks = <List<int>>[]; |
| 24 List<int> dataToWrite = null; | 33 List<int> dataToWrite = null; |
| 25 int bytesWritten = 0; | 34 int bytesWritten = 0; |
| 26 client.writeEventsEnabled = false; | 35 client.writeEventsEnabled = false; |
| 27 client.listen((event) { | 36 client.listen((event) { |
| 28 switch (event) { | 37 switch (event) { |
| 29 case RawSocketEvent.READ: | 38 case RawSocketEvent.READ: |
| 30 Expect.isTrue(bytesWritten == 0); | 39 Expect.isTrue(bytesWritten == 0); |
| 31 Expect.isTrue(client.available() > 0); | 40 Expect.isTrue(client.available() > 0); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 53 } | 62 } |
| 54 }); | 63 }); |
| 55 }); | 64 }); |
| 56 return server; | 65 return server; |
| 57 }); | 66 }); |
| 58 } | 67 } |
| 59 | 68 |
| 60 Future testClient(server) { | 69 Future testClient(server) { |
| 61 Completer success = new Completer(); | 70 Completer success = new Completer(); |
| 62 List<String> chunks = <String>[]; | 71 List<String> chunks = <String>[]; |
| 63 SecureSocket.connect(HOST, server.port).then((socket) { | 72 SecureSocket.connect(HOST, server.port, context: clientContext) |
| 73 .then((socket) { |
| 64 socket.write("Hello server."); | 74 socket.write("Hello server."); |
| 65 socket.close(); | 75 socket.close(); |
| 66 socket.listen( | 76 socket.listen( |
| 67 (List<int> data) { | 77 (List<int> data) { |
| 68 var received = new String.fromCharCodes(data); | 78 var received = new String.fromCharCodes(data); |
| 69 chunks.add(received); | 79 chunks.add(received); |
| 70 }, | 80 }, |
| 71 onDone: () { | 81 onDone: () { |
| 72 String reply = chunks.join(); | 82 String reply = chunks.join(); |
| 73 Expect.equals("Hello server.", reply); | 83 Expect.equals("Hello server.", reply); |
| 74 success.complete(server); | 84 success.complete(server); |
| 75 }); | 85 }); |
| 76 }); | 86 }); |
| 77 return success.future; | 87 return success.future; |
| 78 } | 88 } |
| 79 | 89 |
| 80 void main() { | 90 void main() { |
| 81 asyncStart(); | 91 asyncStart(); |
| 82 String certificateDatabase = Platform.script.resolve('pkcert').toFilePath(); | |
| 83 SecureSocket.initialize(database: certificateDatabase, | |
| 84 password: 'dartdart'); | |
| 85 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) | 92 InternetAddress.lookup("localhost").then((hosts) => HOST = hosts.first) |
| 86 .then((_) => startEchoServer()) | 93 .then((_) => startEchoServer()) |
| 87 .then(testClient) | 94 .then(testClient) |
| 88 .then((server) => server.close()) | 95 .then((server) => server.close()) |
| 89 .then((_) => asyncEnd()); | 96 .then((_) => asyncEnd()); |
| 90 } | 97 } |
| OLD | NEW |