| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Client for https_bad_certificate_test, that runs in a subprocess. | 5 // Client for https_bad_certificate_test, that runs in a subprocess. |
| 6 // It verifies that the client bad certificate callback works in HttpClient. | 6 // It verifies that the client bad certificate callback works in HttpClient. |
| 7 | 7 |
| 8 import "dart:async"; | 8 import "dart:async"; |
| 9 import "dart:io"; | 9 import "dart:io"; |
| 10 | 10 |
| 11 class ExpectException implements Exception { | 11 class ExpectException implements Exception { |
| 12 ExpectException(this.message); | 12 ExpectException(this.message); |
| 13 String toString() => "ExpectException: $message"; | 13 String toString() => "ExpectException: $message"; |
| 14 String message; | 14 String message; |
| 15 } | 15 } |
| 16 | 16 |
| 17 void expect(condition) { | 17 void expect(condition) { |
| 18 if (!condition) { | 18 if (!condition) { |
| 19 throw new ExpectException(''); | 19 throw new ExpectException(''); |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 | 22 |
| 23 const HOST_NAME = "localhost"; | 23 const HOST_NAME = "localhost"; |
| 24 | 24 |
| 25 Future runHttpClient(int port, result) { | 25 Future runHttpClient(int port, result) async { |
| 26 bool badCertificateCallback(X509Certificate certificate, | 26 bool badCertificateCallback(X509Certificate certificate, |
| 27 String host, | 27 String host, |
| 28 int callbackPort) { | 28 int callbackPort) { |
| 29 expect(HOST_NAME == host); | 29 expect(HOST_NAME == host); |
| 30 expect(callbackPort == port); | 30 expect(callbackPort == port); |
| 31 expect('CN=localhost' == certificate.subject); | 31 expect('CN=localhost' == certificate.subject); |
| 32 expect('CN=myauthority' == certificate.issuer); | 32 expect('CN=myauthority' == certificate.issuer); |
| 33 expect(result != 'exception'); // Throw exception if one is requested. | 33 expect(result != 'exception'); // Throw exception if one is requested. |
| 34 if (result == 'true') return true; | 34 if (result == 'true') return true; |
| 35 if (result == 'false') return false; | 35 if (result == 'false') return false; |
| 36 return result; | 36 return result; |
| 37 } | 37 } |
| 38 | 38 |
| 39 HttpClient client = new HttpClient(); | 39 HttpClient client = new HttpClient(); |
| 40 | 40 |
| 41 var testFutures = []; // The three async getUrl calls run simultaneously. | 41 await client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) |
| 42 testFutures.add(client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) | |
| 43 .then((HttpClientRequest request) { | 42 .then((HttpClientRequest request) { |
| 44 expect(result == 'true'); // The session cache may keep the session. | 43 expect(result == 'true'); // The session cache may keep the session. |
| 45 return request.close(); | 44 return request.close(); |
| 46 }, onError: (e) { | 45 }, onError: (e) { |
| 47 expect(e is HandshakeException || e is SocketException); | 46 expect(e is HandshakeException || e is SocketException); |
| 48 })); | 47 }); |
| 49 | 48 |
| 50 client.badCertificateCallback = badCertificateCallback; | 49 client.badCertificateCallback = badCertificateCallback; |
| 51 testFutures.add(client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) | 50 await client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) |
| 52 .then((HttpClientRequest request) { | 51 .then((HttpClientRequest request) { |
| 53 expect(result == 'true'); | 52 expect(result == 'true'); |
| 54 return request.close(); | 53 return request.close(); |
| 55 }, onError: (e) { | 54 }, onError: (e) { |
| 56 if (result == 'false') expect (e is HandshakeException || | 55 if (result == 'false') expect (e is HandshakeException || |
| 57 e is SocketException); | 56 e is SocketException); |
| 58 else if (result == 'exception') expect (e is ExpectException || | 57 else if (result == 'exception') expect (e is ExpectException || |
| 59 e is SocketException); | 58 e is SocketException); |
| 60 else expect (e is ArgumentError || e is SocketException); | 59 else { |
| 61 })); | 60 expect (e is ArgumentError || e is SocketException); |
| 61 } |
| 62 }); |
| 62 | 63 |
| 63 client.badCertificateCallback = null; | 64 client.badCertificateCallback = null; |
| 64 testFutures.add(client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) | 65 await client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) |
| 65 .then((HttpClientRequest request) { | 66 .then((HttpClientRequest request) { |
| 66 expect(result == 'true'); // The session cache may keep the session. | 67 expect(result == 'true'); // The session cache may keep the session. |
| 67 return request.close(); | 68 return request.close(); |
| 68 }, onError: (e) { | 69 }, onError: (e) { |
| 69 expect(e is HandshakeException || e is SocketException); | 70 expect(e is HandshakeException || e is SocketException); |
| 70 })); | 71 }); |
| 71 | 72 |
| 72 return Future.wait(testFutures).then((_) => client.close()); | 73 client.close(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void main(List<String> args) { | 76 void main(List<String> args) { |
| 76 SecureSocket.initialize(); | 77 SecureSocket.initialize(); |
| 77 int port = int.parse(args[0]); | 78 int port = int.parse(args[0]); |
| 78 runHttpClient(port, args[1]) | 79 runHttpClient(port, args[1]) |
| 79 .then((_) => print('SUCCESS')); | 80 .then((_) => print('SUCCESS')); |
| 80 } | 81 } |
| OLD | NEW |