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