| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 |
| 8 // VMOptions=--short_socket_write --short_socket_read |
| 7 // The --short_socket_write option does not work with external server | 9 // The --short_socket_write option does not work with external server |
| 8 // www.google.dk. Add this to the test when we have secure server sockets. | 10 // www.google.dk. Add this to the test when we have secure server sockets. |
| 9 // See TODO below. | 11 // See TODO below. |
| 10 | 12 |
| 11 import "dart:async"; | 13 import "dart:async"; |
| 12 import "dart:isolate"; | 14 import "dart:isolate"; |
| 13 import "dart:io"; | 15 import "dart:io"; |
| 14 | 16 |
| 15 void WriteAndClose(Socket socket, String message) { | |
| 16 var data = message.charCodes; | |
| 17 int written = 0; | |
| 18 void write() { | |
| 19 written += socket.writeList(data, written, data.length - written); | |
| 20 if (written < data.length) { | |
| 21 socket.onWrite = write; | |
| 22 } else { | |
| 23 socket.close(true); | |
| 24 } | |
| 25 } | |
| 26 write(); | |
| 27 } | |
| 28 | |
| 29 void main() { | 17 void main() { |
| 18 ReceivePort keepAlive = new ReceivePort(); |
| 30 SecureSocket.initialize(useBuiltinRoots: false); | 19 SecureSocket.initialize(useBuiltinRoots: false); |
| 31 testCertificateCallback(host: "www.google.dk", | 20 testCertificateCallback(host: "www.google.dk", |
| 32 acceptCertificate: false).then((_) { | 21 acceptCertificate: false).then((_) { |
| 33 testCertificateCallback(host: "www.google.dk", | 22 testCertificateCallback(host: "www.google.dk", |
| 34 acceptCertificate: true).then((_) { | 23 acceptCertificate: true).then((_) { |
| 24 keepAlive.close(); |
| 35 // TODO(7153): Open a receive port, and close it when we get here. | 25 // TODO(7153): Open a receive port, and close it when we get here. |
| 36 // Currently, it can happen that neither onClosed or onError is called. | 26 // Currently, it can happen that neither onClosed or onError is called. |
| 37 // So we never reach this point. Diagnose this and fix. | 27 // So we never reach this point. Diagnose this and fix. |
| 38 }); | 28 }); |
| 39 }); | 29 }); |
| 40 } | 30 } |
| 41 | 31 |
| 42 Future testCertificateCallback({String host, bool acceptCertificate}) { | 32 Future testCertificateCallback({String host, bool acceptCertificate}) { |
| 43 Completer completer = new Completer(); | 33 Expect.throws( |
| 44 var secure = new SecureSocket(host, 443); | 34 () { |
| 45 List<String> chunks = <String>[]; | 35 var x = 7; |
| 46 secure.onConnect = () { | 36 SecureSocket.connect(host, 443, onBadCertificate: x); |
| 47 Expect.isTrue(acceptCertificate); | 37 }, |
| 48 WriteAndClose(secure, "GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); | 38 (e) => e is ArgumentError || e is TypeError); |
| 49 }; | 39 |
| 50 secure.onBadCertificate = (_) { }; | 40 bool badCertificateCallback(X509Certificate certificate) { |
| 51 secure.onBadCertificate = null; | |
| 52 Expect.throws(() => secure.onBadCertificate = 7, | |
| 53 (e) => e is TypeError || e is SocketIOException); | |
| 54 secure.onBadCertificate = (X509Certificate certificate) { | |
| 55 Expect.isTrue(certificate.subject.contains("O=Google Inc")); | 41 Expect.isTrue(certificate.subject.contains("O=Google Inc")); |
| 56 Expect.isTrue(certificate.startValidity < new DateTime.now()); | 42 Expect.isTrue(certificate.startValidity < new DateTime.now()); |
| 57 Expect.isTrue(certificate.endValidity > new DateTime.now()); | 43 Expect.isTrue(certificate.endValidity > new DateTime.now()); |
| 58 return acceptCertificate; | 44 return acceptCertificate; |
| 59 }; | 45 }; |
| 60 secure.onData = () { | 46 |
| 61 Expect.isTrue(acceptCertificate); | 47 return SecureSocket.connect(host, |
| 62 chunks.add(new String.fromCharCodes(secure.read())); | 48 443, |
| 63 }; | 49 onBadCertificate: badCertificateCallback) |
| 64 secure.onClosed = () { | 50 .then((socket) { |
| 65 Expect.isTrue(acceptCertificate); | 51 Expect.isTrue(acceptCertificate); |
| 66 String fullPage = chunks.join(); | 52 socket.add("GET / HTTP/1.0\r\nHost: $host\r\n\r\n".charCodes); |
| 67 Expect.isTrue(fullPage.contains('</body></html>')); | 53 socket.close(); |
| 68 completer.complete(null); | 54 return socket.reduce(<int>[], (message, data) => message..addAll(data)) |
| 69 }; | 55 .then((message) { |
| 70 secure.onError = (e) { | 56 String received = new String.fromCharCodes(message); |
| 71 Expect.isFalse(acceptCertificate); | 57 Expect.isTrue(received.contains('</body></html>')); |
| 72 completer.complete(null); | 58 }); |
| 73 }; | 59 }).catchError((e) { |
| 74 return completer.future; | 60 Expect.isFalse(acceptCertificate); |
| 61 }); |
| 75 } | 62 } |
| OLD | NEW |