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