Chromium Code Reviews| Index: tests/standalone/io/secure_socket_bad_certificate_test.dart |
| diff --git a/tests/standalone/io/secure_socket_bad_certificate_test.dart b/tests/standalone/io/secure_socket_bad_certificate_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e91972c992b62976a4d66f6ce1b0f9fd0242f705 |
| --- /dev/null |
| +++ b/tests/standalone/io/secure_socket_bad_certificate_test.dart |
| @@ -0,0 +1,77 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| +// |
| +// VMOptions= |
| +// VMOptions=--short_socket_read |
| +// The --short_socket_write option does not work with external server |
| +// www.google.dk. Add this to the test when we have secure server sockets. |
| +// See TODO below. |
| + |
| +#import("dart:isolate"); |
| +#import("dart:io"); |
| + |
| +void WriteAndClose(Socket socket, String message) { |
| + var data = message.charCodes; |
| + int written = 0; |
| + void write() { |
| + written += socket.writeList(data, written, data.length - written); |
| + if (written < data.length) { |
| + socket.onWrite = write; |
| + } else { |
| + socket.close(true); |
| + } |
| + } |
| + write(); |
| +} |
| + |
| +void main() { |
| + SecureSocket.initialize(useBuiltinRoots: false); |
| + testCertificateCallback(host: "www.google.dk", |
| + acceptCertificate: false, |
| + then: (){ |
|
Mads Ager (google)
2012/12/05 07:30:53
Add space: () {
testCertificateCallback should re
|
| + testCertificateCallback(host: "www.google.dk", |
| + acceptCertificate: true, |
| + then: (){ |
|
Mads Ager (google)
2012/12/05 07:30:53
Ditto
|
| + // TODO(whesse): Open a receive port, and close it when we get here. |
|
Mads Ager (google)
2012/12/05 07:30:53
This is bad. Please file a high-priority bug repor
Bill Hesse
2012/12/05 12:35:38
Done.
|
| + // Currently, it can happen that neither onClosed or onError is called. |
| + // So we never reach this point. Diagnose this and fix. |
| + }); |
| + }); |
| +} |
| + |
| +void testCertificateCallback({String host, |
| + bool acceptCertificate, |
| + Function then}) { |
| + var secure = new SecureSocket(host, 443); |
| + List<String> chunks = <String>[]; |
| + secure.onConnect = () { |
| + Expect.isTrue(acceptCertificate); |
| + WriteAndClose(secure, "GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); |
| + }; |
| + secure.onBadCertificate = (_) { }; |
| + secure.onBadCertificate = null; |
| + Expect.throws(() => secure.onBadCertificate = 7, |
| + (e) => e is TypeError || e is SocketIOException); |
| + secure.onBadCertificate = (X509Certificate certificate) { |
| + Expect.isTrue(certificate.subject.contains("O=Google Inc")); |
| + |
|
Mads Ager (google)
2012/12/05 07:30:53
Remove empty line?
Bill Hesse
2012/12/05 12:35:38
Done.
|
| + Expect.isTrue(certificate.startValidity < new Date.now()); |
| + Expect.isTrue(certificate.endValidity > new Date.now()); |
| + return acceptCertificate; |
| + }; |
| + secure.onData = (){ |
| + Expect.isTrue(acceptCertificate); |
| + chunks.add(new String.fromCharCodes(secure.read())); |
| + }; |
| + secure.onClosed = () { |
| + Expect.isTrue(acceptCertificate); |
| + String fullPage = Strings.concatAll(chunks); |
| + Expect.isTrue(fullPage.contains('</body></html>')); |
| + then(); |
| + }; |
| + secure.onError = (e) { |
| + Expect.isFalse(acceptCertificate); |
| + then(); |
| + }; |
| +} |