| 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_write --short_socket_read | 8 // VMOptions=--short_socket_write --short_socket_read |
| 9 // The --short_socket_write option does not work with external server | 9 // The --short_socket_write option does not work with external server |
| 10 // 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. |
| 11 // See TODO below. | 11 // See TODO below. |
| 12 | 12 |
| 13 import "package:expect/expect.dart"; | 13 import "package:expect/expect.dart"; |
| 14 import "dart:async"; | 14 import "dart:async"; |
| 15 import "dart:isolate"; | 15 import "dart:isolate"; |
| 16 import "dart:io"; | 16 import "dart:io"; |
| 17 | 17 |
| 18 void main() { | 18 void main() { |
| 19 ReceivePort keepAlive = new ReceivePort(); | 19 ReceivePort keepAlive = new ReceivePort(); |
| 20 SecureSocket.initialize(useBuiltinRoots: false); | 20 SecureSocket.initialize(useBuiltinRoots: false); |
| 21 testCertificateCallback(host: "www.google.dk", | 21 testCertificateCallback(host: "www.google.com", |
| 22 acceptCertificate: false).then((_) { | 22 acceptCertificate: false) |
| 23 testCertificateCallback(host: "www.google.dk", | 23 .then((_) => |
| 24 acceptCertificate: true).then((_) { | 24 testCertificateCallback(host: "www.google.com", |
| 25 keepAlive.close(); | 25 acceptCertificate: true)) |
| 26 // TODO(7153): Open a receive port, and close it when we get here. | 26 .then((_) => |
| 27 // Currently, it can happen that neither onClosed or onError is called. | 27 keepAlive.close()); |
| 28 // So we never reach this point. Diagnose this and fix. | |
| 29 }); | |
| 30 }); | |
| 31 } | 28 } |
| 32 | 29 |
| 33 Future testCertificateCallback({String host, bool acceptCertificate}) { | 30 Future testCertificateCallback({String host, bool acceptCertificate}) { |
| 34 var x = 7; | 31 var x = 7; |
| 35 Expect.throws(() => SecureSocket.connect(host, 443, onBadCertificate: x), | 32 Expect.throws(() => SecureSocket.connect(host, 443, onBadCertificate: x), |
| 36 (e) => e is ArgumentError || e is TypeError); | 33 (e) => e is ArgumentError || e is TypeError); |
| 37 | 34 |
| 38 bool badCertificateCallback(X509Certificate certificate) { | 35 bool badCertificateCallback(X509Certificate certificate) { |
| 39 Expect.isTrue(certificate.subject.contains("O=Google Inc")); | 36 Expect.isTrue(certificate.subject.contains("O=Google Inc")); |
| 40 Expect.isTrue(certificate.startValidity.isBefore(new DateTime.now())); | 37 Expect.isTrue(certificate.startValidity.isBefore(new DateTime.now())); |
| 41 Expect.isTrue(certificate.endValidity.isAfter(new DateTime.now())); | 38 Expect.isTrue(certificate.endValidity.isAfter(new DateTime.now())); |
| 42 return acceptCertificate; | 39 return acceptCertificate; |
| 43 }; | 40 }; |
| 44 | 41 |
| 45 return SecureSocket.connect(host, | 42 return SecureSocket.connect(host, |
| 46 443, | 43 443, |
| 47 onBadCertificate: badCertificateCallback) | 44 onBadCertificate: badCertificateCallback) |
| 48 .then((socket) { | 45 .then((socket) { |
| 49 Expect.isTrue(acceptCertificate); | 46 Expect.isTrue(acceptCertificate); |
| 50 socket.write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); | 47 socket.write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); |
| 51 socket.close(); | 48 socket.close(); |
| 52 return socket.fold(<int>[], (message, data) => message..addAll(data)) | 49 return socket.fold(<int>[], (message, data) => message..addAll(data)) |
| 53 .then((message) { | 50 .then((message) { |
| 54 String received = new String.fromCharCodes(message); | 51 String received = new String.fromCharCodes(message); |
| 55 Expect.isTrue(received.contains('</body></html>')); | 52 Expect.isTrue(received.startsWith('HTTP/1.0 ')); |
| 56 }); | 53 }); |
| 57 }).catchError((e) { | 54 }).catchError((e) { |
| 58 Expect.isFalse(acceptCertificate); | 55 if (e is HandshakeException) { |
| 56 Expect.isFalse(acceptCertificate); |
| 57 } else { |
| 58 Expect.isTrue(e is SocketException); |
| 59 } |
| 59 }); | 60 }); |
| 60 } | 61 } |
| OLD | NEW |