| 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 https_bad_certificate_test, that runs in a subprocess. |
| 6 // It verifies that the client bad certificate callback works in HttpClient. |
| 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 Future runHttpClient(int port, result) { |
| 26 bool badCertificateCallback(X509Certificate certificate, |
| 27 String host, |
| 28 int callbackPort) { |
| 29 expect(HOST_NAME == host); |
| 30 expect(callbackPort == port); |
| 31 expect('CN=localhost' == certificate.subject); |
| 32 expect('CN=myauthority' == certificate.issuer); |
| 33 expect(result != 'exception'); // Throw exception if one is requested. |
| 34 if (result == 'true') return true; |
| 35 if (result == 'false') return false; |
| 36 return result; |
| 37 } |
| 38 |
| 39 HttpClient client = new HttpClient(); |
| 40 |
| 41 var testFutures = []; |
| 42 testFutures.add(client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) |
| 43 .then((HttpClientRequest request) { expect(false); }, |
| 44 onError: (e) { expect(e is HandshakeException); })); |
| 45 |
| 46 client.badCertificateCallback = badCertificateCallback; |
| 47 testFutures.add( client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) |
| 48 .then((HttpClientRequest request) { |
| 49 expect(result == 'true'); |
| 50 request.close().then((result) { }); |
| 51 }, onError: (e) { |
| 52 if (result == 'false') expect (e is HandshakeException); |
| 53 else if (result == 'exception') expect (e is ExpectException); |
| 54 else expect (e is ArgumentError); |
| 55 })); |
| 56 |
| 57 client.badCertificateCallback = null; |
| 58 testFutures.add(client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) |
| 59 .then((HttpClientRequest request) { |
| 60 expect(false); |
| 61 }, onError: (e) { |
| 62 expect(e is HandshakeException); |
| 63 })); |
| 64 |
| 65 return Future.wait(testFutures); |
| 66 } |
| 67 |
| 68 void main() { |
| 69 final args = new Options().arguments; |
| 70 SecureSocket.initialize(); |
| 71 int port = int.parse(args[0]); |
| 72 runHttpClient(port, args[1]) |
| 73 .then((_) => print('SUCCESS')); |
| 74 } |
| OLD | NEW |