Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: tests/standalone/io/secure_socket_bad_certificate_test.dart

Issue 11415290: Add a callback to SecureSocket for certificates that fail to be authenticated. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Switch to Futures in test. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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,
42 bool acceptCertificate,
43 Function then}) {
Mads Ager (google) 2012/12/05 13:08:03 Delete the 'then' parameter now that you return a
Bill Hesse 2012/12/05 14:31:06 Done.
44 Completer completer = new Completer();
45 var secure = new SecureSocket(host, 443);
46 List<String> chunks = <String>[];
47 secure.onConnect = () {
48 Expect.isTrue(acceptCertificate);
49 WriteAndClose(secure, "GET / HTTP/1.0\r\nHost: $host\r\n\r\n");
50 };
51 secure.onBadCertificate = (_) { };
52 secure.onBadCertificate = null;
53 Expect.throws(() => secure.onBadCertificate = 7,
54 (e) => e is TypeError || e is SocketIOException);
55 secure.onBadCertificate = (X509Certificate certificate) {
56 Expect.isTrue(certificate.subject.contains("O=Google Inc"));
57 Expect.isTrue(certificate.startValidity < new Date.now());
58 Expect.isTrue(certificate.endValidity > new Date.now());
59 return acceptCertificate;
60 };
61 secure.onData = (){
Mads Ager (google) 2012/12/05 13:08:03 Add a space before the {
Bill Hesse 2012/12/05 14:31:06 Done.
62 Expect.isTrue(acceptCertificate);
63 chunks.add(new String.fromCharCodes(secure.read()));
64 };
65 secure.onClosed = () {
66 Expect.isTrue(acceptCertificate);
67 String fullPage = Strings.concatAll(chunks);
68 Expect.isTrue(fullPage.contains('</body></html>'));
69 completer.complete(null);
70 };
71 secure.onError = (e) {
72 Expect.isFalse(acceptCertificate);
73 completer.complete(null);
74 };
75 return completer.future;
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698