OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // VMOptions= | |
6 // VMOptions=--short_socket_read | |
7 // VMOptions=--short_socket_write | |
8 // VMOptions=--short_socket_write --short_socket_read | |
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. | |
11 // See TODO below. | |
12 | |
13 import "package:expect/expect.dart"; | |
14 import "dart:async"; | |
15 import "dart:isolate"; | |
16 import "dart:io"; | |
17 | |
18 void main() { | |
19 ReceivePort keepAlive = new ReceivePort(); | |
20 SecureSocket.initialize(useBuiltinRoots: false); | |
21 testCertificateCallback(host: "www.google.com", | |
22 acceptCertificate: false) | |
23 .then((_) => | |
24 testCertificateCallback(host: "www.google.com", | |
25 acceptCertificate: true)) | |
26 .then((_) => | |
27 keepAlive.close()); | |
28 } | |
29 | |
30 Future testCertificateCallback({String host, bool acceptCertificate}) { | |
31 var x = 7; | |
32 Expect.throws(() => SecureSocket.connect(host, 443, onBadCertificate: x), | |
33 (e) => e is ArgumentError || e is TypeError); | |
34 | |
35 bool badCertificateCallback(X509Certificate certificate) { | |
36 Expect.isTrue(certificate.subject.contains("O=Google Inc")); | |
37 Expect.isTrue(certificate.startValidity.isBefore(new DateTime.now())); | |
38 Expect.isTrue(certificate.endValidity.isAfter(new DateTime.now())); | |
39 return acceptCertificate; | |
40 }; | |
41 | |
42 return SecureSocket.connect(host, | |
43 443, | |
44 onBadCertificate: badCertificateCallback) | |
45 .then((socket) { | |
46 Expect.isTrue(acceptCertificate); | |
47 socket.write("GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); | |
48 socket.close(); | |
49 return socket.fold(<int>[], (message, data) => message..addAll(data)) | |
50 .then((message) { | |
51 String received = new String.fromCharCodes(message); | |
52 Expect.isTrue(received.startsWith('HTTP/1.0 ')); | |
53 }); | |
54 }).catchError((e) { | |
55 if (e is HandshakeException) { | |
56 Expect.isFalse(acceptCertificate); | |
57 } else { | |
58 Expect.isTrue(e is SocketException); | |
59 } | |
60 }); | |
61 } | |
OLD | NEW |