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 // Client for secure_bad_certificate_test, that runs in a subprocess. |
| 6 // The test verifies that the client bad certificate callback works. |
| 7 |
| 8 import "dart:async"; |
| 9 import "dart:io"; |
| 10 |
| 11 class ExpectException implements Exception { |
| 12 ExpectException(this.message); |
| 13 String toString() => "ExpectException: $message"; |
| 14 String message; |
| 15 } |
| 16 |
| 17 void expect(condition) { |
| 18 if (!condition) { |
| 19 throw new ExpectException(''); |
| 20 } |
| 21 } |
| 22 |
| 23 const HOST_NAME = "localhost"; |
| 24 |
| 25 void runClient(int port, result) { |
| 26 bool badCertificateCallback(X509Certificate certificate) { |
| 27 expect('CN=localhost' == certificate.subject); |
| 28 expect('CN=myauthority' == certificate.issuer); |
| 29 expect(result != 'exception'); // Throw exception if one is requested. |
| 30 if (result == 'true') result = true; |
| 31 if (result == 'false') result = false; |
| 32 return result; |
| 33 } |
| 34 |
| 35 SecureSocket.connect(HOST_NAME, |
| 36 port, |
| 37 onBadCertificate: badCertificateCallback) |
| 38 .then((SecureSocket socket) { |
| 39 expect(result); |
| 40 socket.close(); |
| 41 }, |
| 42 onError: (error) { |
| 43 expect(result != true); |
| 44 if (result == false) { |
| 45 expect(error is HandshakeException); |
| 46 } else if (result == 'exception') { |
| 47 expect(error is ExpectException); |
| 48 } else { |
| 49 expect(error is ArgumentError); |
| 50 } |
| 51 }); |
| 52 } |
| 53 |
| 54 |
| 55 void main() { |
| 56 final args = new Options().arguments; |
| 57 SecureSocket.initialize(); |
| 58 runClient(int.parse(args[0]), args[1]); |
| 59 } |
OLD | NEW |