| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // 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. |
| 9 // See TODO below. |
| 10 |
| 11 #import("dart:isolate"); |
| 12 #import("dart:io"); |
| 13 |
| 14 void WriteAndClose(Socket socket, String message) { |
| 15 var data = message.charCodes; |
| 16 int written = 0; |
| 17 void write() { |
| 18 written += socket.writeList(data, written, data.length - written); |
| 19 if (written < data.length) { |
| 20 socket.onWrite = write; |
| 21 } else { |
| 22 socket.close(true); |
| 23 } |
| 24 } |
| 25 write(); |
| 26 } |
| 27 |
| 28 void main() { |
| 29 SecureSocket.initialize(useBuiltinRoots: false); |
| 30 testCertificateCallback(host: "www.google.dk", |
| 31 acceptCertificate: false).then((_) { |
| 32 testCertificateCallback(host: "www.google.dk", |
| 33 acceptCertificate: true).then((_) { |
| 34 // TODO(7153): Open a receive port, and close it when we get here. |
| 35 // Currently, it can happen that neither onClosed or onError is called. |
| 36 // So we never reach this point. Diagnose this and fix. |
| 37 }); |
| 38 }); |
| 39 } |
| 40 |
| 41 Future testCertificateCallback({String host, bool acceptCertificate}) { |
| 42 Completer completer = new Completer(); |
| 43 var secure = new SecureSocket(host, 443); |
| 44 List<String> chunks = <String>[]; |
| 45 secure.onConnect = () { |
| 46 Expect.isTrue(acceptCertificate); |
| 47 WriteAndClose(secure, "GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); |
| 48 }; |
| 49 secure.onBadCertificate = (_) { }; |
| 50 secure.onBadCertificate = null; |
| 51 Expect.throws(() => secure.onBadCertificate = 7, |
| 52 (e) => e is TypeError || e is SocketIOException); |
| 53 secure.onBadCertificate = (X509Certificate certificate) { |
| 54 Expect.isTrue(certificate.subject.contains("O=Google Inc")); |
| 55 Expect.isTrue(certificate.startValidity < new Date.now()); |
| 56 Expect.isTrue(certificate.endValidity > new Date.now()); |
| 57 return acceptCertificate; |
| 58 }; |
| 59 secure.onData = () { |
| 60 Expect.isTrue(acceptCertificate); |
| 61 chunks.add(new String.fromCharCodes(secure.read())); |
| 62 }; |
| 63 secure.onClosed = () { |
| 64 Expect.isTrue(acceptCertificate); |
| 65 String fullPage = Strings.concatAll(chunks); |
| 66 Expect.isTrue(fullPage.contains('</body></html>')); |
| 67 completer.complete(null); |
| 68 }; |
| 69 secure.onError = (e) { |
| 70 Expect.isFalse(acceptCertificate); |
| 71 completer.complete(null); |
| 72 }; |
| 73 return completer.future; |
| 74 } |
| OLD | NEW |