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) async { | |
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 await client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) | |
42 .then((HttpClientRequest request) { | |
43 expect(result == 'true'); // The session cache may keep the session. | |
44 return request.close(); | |
45 }, onError: (e) { | |
46 expect(e is HandshakeException || e is SocketException); | |
47 }); | |
48 | |
49 client.badCertificateCallback = badCertificateCallback; | |
50 await client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) | |
51 .then((HttpClientRequest request) { | |
52 expect(result == 'true'); | |
53 return request.close(); | |
54 }, onError: (e) { | |
55 if (result == 'false') expect (e is HandshakeException || | |
56 e is SocketException); | |
57 else if (result == 'exception') expect (e is ExpectException || | |
58 e is SocketException); | |
59 else { | |
60 expect (e is ArgumentError || e is SocketException); | |
61 } | |
62 }); | |
63 | |
64 client.badCertificateCallback = null; | |
65 await client.getUrl(Uri.parse('https://$HOST_NAME:$port/$result')) | |
66 .then((HttpClientRequest request) { | |
67 expect(result == 'true'); // The session cache may keep the session. | |
68 return request.close(); | |
69 }, onError: (e) { | |
70 expect(e is HandshakeException || e is SocketException); | |
71 }); | |
72 | |
73 client.close(); | |
74 } | |
75 | |
76 void main(List<String> args) { | |
77 SecureSocket.initialize(); | |
78 int port = int.parse(args[0]); | |
79 runHttpClient(port, args[1]) | |
80 .then((_) => print('SUCCESS')); | |
81 } | |
OLD | NEW |