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 // This test verifies that the bad certificate callback works in HttpClient. | 5 // This test verifies that the bad certificate callback works in HttpClient. |
6 | 6 |
7 import "package:expect/expect.dart"; | |
8 import "package:path/path.dart"; | |
9 import "dart:async"; | 7 import "dart:async"; |
10 import "dart:io"; | 8 import "dart:io"; |
11 | 9 |
12 const HOST_NAME = "localhost"; | 10 import "package:expect/expect.dart"; |
13 const CERTIFICATE = "localhost_cert"; | |
14 | 11 |
15 Future<SecureServerSocket> runServer() { | 12 final HOST_NAME = 'localhost'; |
16 SecureSocket.initialize( | 13 |
17 database: Platform.script.resolve('pkcert').toFilePath(), | 14 String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 15 |
| 16 SecurityContext serverContext = new SecurityContext() |
| 17 ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 18 ..usePrivateKey(localFile('certificates/server_key.pem'), |
18 password: 'dartdart'); | 19 password: 'dartdart'); |
19 | 20 |
20 return HttpServer.bindSecure( | 21 class CustomException {} |
21 HOST_NAME, 0, backlog: 5, certificateName: 'localhost_cert') | 22 |
22 .then((server) { | 23 main() async { |
23 server.listen((HttpRequest request) { | 24 var HOST = (await InternetAddress.lookup(HOST_NAME)).first; |
24 request.listen((_) { }, onDone: () { request.response.close(); }); | 25 var server = await HttpServer.bindSecure(HOST, 0, serverContext, backlog: 5); |
25 }, onError: (e) { if (e is! HandshakeException) throw e; }); | 26 server.listen((request) { |
26 return server; | 27 request.listen((_) { |
| 28 }, onDone: () { |
| 29 request.response.close(); |
| 30 }); |
27 }); | 31 }); |
| 32 |
| 33 SecurityContext goodContext = new SecurityContext() |
| 34 ..setTrustedCertificates(file: localFile('certificates/trusted_certs.pem')); |
| 35 SecurityContext badContext = new SecurityContext(); |
| 36 SecurityContext defaultContext = SecurityContext.defaultContext; |
| 37 |
| 38 await runClient(server.port, goodContext, true, 'pass'); |
| 39 await runClient(server.port, goodContext, false, 'pass'); |
| 40 await runClient(server.port, goodContext, 'fisk', 'pass'); |
| 41 await runClient(server.port, goodContext, 'exception', 'pass'); |
| 42 await runClient(server.port, badContext, true, 'pass'); |
| 43 await runClient(server.port, badContext, false, 'fail'); |
| 44 await runClient(server.port, badContext, 'fisk', 'fail'); |
| 45 await runClient(server.port, badContext, 'exception', 'throw'); |
| 46 await runClient(server.port, defaultContext, true, 'pass'); |
| 47 await runClient(server.port, defaultContext, false, 'fail'); |
| 48 await runClient(server.port, defaultContext, 'fisk', 'fail'); |
| 49 await runClient(server.port, defaultContext, 'exception', 'throw'); |
| 50 server.close(); |
28 } | 51 } |
29 | 52 |
30 main() async { | 53 |
31 var clientScript = Platform.script | 54 Future runClient(int port, |
32 .resolve('https_bad_certificate_client.dart') | 55 SecurityContext context, |
33 .toFilePath(); | 56 callbackReturns, |
34 Future clientProcess(int port, String acceptCertificate) { | 57 result) async { |
35 return Process.run(Platform.executable, | 58 HttpClient client = new HttpClient(context: context); |
36 [clientScript, port.toString(), acceptCertificate]) | 59 client.badCertificateCallback = (X509Certificate certificate, host, port) { |
37 .then((ProcessResult result) { | 60 Expect.equals('/CN=rootauthority', certificate.subject); |
38 if (result.exitCode != 0 || !result.stdout.contains('SUCCESS')) { | 61 Expect.equals('/CN=rootauthority', certificate.issuer); |
39 print("Client failed, acceptCertificate: $acceptCertificate"); | 62 // Throw exception if one is requested. |
40 print(" stdout:"); | 63 if (callbackReturns == 'exception') throw new CustomException(); |
41 print(result.stdout); | 64 return callbackReturns; |
42 print(" stderr:"); | 65 }; |
43 print(result.stderr); | 66 |
44 Expect.fail('Client subprocess exit code: ${result.exitCode}'); | 67 try { |
45 } | 68 var request = await client.getUrl(Uri.parse('https://$HOST_NAME:$port/')); |
46 }); | 69 Expect.equals('pass', result); |
| 70 await request.close(); |
| 71 } catch (error) { |
| 72 Expect.notEquals(result, 'pass'); |
| 73 if (result == 'fail') { |
| 74 Expect.isTrue(error is HandshakeException); |
| 75 } else if (result == 'throw') { |
| 76 Expect.isTrue(error is CustomException); |
| 77 } else { |
| 78 Expect.fail('Unknown expectation $result'); |
| 79 } |
47 } | 80 } |
48 | |
49 var server = await runServer(); | |
50 await clientProcess(server.port, 'true'); | |
51 await clientProcess(server.port, 'false'); | |
52 await clientProcess(server.port, 'fisk'); | |
53 await clientProcess(server.port, 'exception'); | |
54 server.close(); | |
55 } | 81 } |
OLD | NEW |