| 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. | 9 // This test verifies that the bad certificate callback works. |
| 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 SecureServerSocket.bind(HOST_NAME, 0, serverContext); | 29 var server = await SecureServerSocket.bind(HOST_NAME, 0, serverContext); |
| 30 server.listen((SecureSocket socket) { | 30 server.listen((SecureSocket socket) { |
| 31 socket.listen((_) {}, onDone: () { | 31 socket.listen((_) {}, onDone: () { |
| 32 socket.close(); | 32 socket.close(); |
| 33 }); | 33 }); |
| 34 }, onError: (e) { if (e is! HandshakeException) throw e; }); | 34 }, onError: (e) { |
| 35 if (e is! HandshakeException) throw e; |
| 36 }); |
| 35 | 37 |
| 36 SecurityContext goodContext = new SecurityContext() | 38 SecurityContext goodContext = new SecurityContext() |
| 37 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); | 39 ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
| 38 SecurityContext badContext = new SecurityContext(); | 40 SecurityContext badContext = new SecurityContext(); |
| 39 SecurityContext defaultContext = SecurityContext.defaultContext; | 41 SecurityContext defaultContext = SecurityContext.defaultContext; |
| 40 | 42 |
| 41 await runClient(server.port, goodContext, true, 'pass'); | 43 await runClient(server.port, goodContext, true, 'pass'); |
| 42 await runClient(server.port, goodContext, false, 'pass'); | 44 await runClient(server.port, goodContext, false, 'pass'); |
| 43 await runClient(server.port, goodContext, 'fisk', 'pass'); | 45 await runClient(server.port, goodContext, 'fisk', 'pass'); |
| 44 await runClient(server.port, goodContext, 'exception', 'pass'); | 46 await runClient(server.port, goodContext, 'exception', 'pass'); |
| 45 await runClient(server.port, badContext, true, 'pass'); | 47 await runClient(server.port, badContext, true, 'pass'); |
| 46 await runClient(server.port, badContext, false, 'fail'); | 48 await runClient(server.port, badContext, false, 'fail'); |
| 47 await runClient(server.port, badContext, 'fisk', 'fail'); | 49 await runClient(server.port, badContext, 'fisk', 'fail'); |
| 48 await runClient(server.port, badContext, 'exception', 'throw'); | 50 await runClient(server.port, badContext, 'exception', 'throw'); |
| 49 await runClient(server.port, defaultContext, true, 'pass'); | 51 await runClient(server.port, defaultContext, true, 'pass'); |
| 50 await runClient(server.port, defaultContext, false, 'fail'); | 52 await runClient(server.port, defaultContext, false, 'fail'); |
| 51 await runClient(server.port, defaultContext, 'fisk', 'fail'); | 53 await runClient(server.port, defaultContext, 'fisk', 'fail'); |
| 52 await runClient(server.port, defaultContext, 'exception', 'throw'); | 54 await runClient(server.port, defaultContext, 'exception', 'throw'); |
| 53 server.close(); | 55 server.close(); |
| 54 } | 56 } |
| 55 | 57 |
| 56 | 58 Future runClient( |
| 57 Future runClient(int port, | 59 int port, SecurityContext context, callbackReturns, result) async { |
| 58 SecurityContext context, | |
| 59 callbackReturns, | |
| 60 result) async { | |
| 61 badCertificateCallback(X509Certificate certificate) { | 60 badCertificateCallback(X509Certificate certificate) { |
| 62 Expect.isTrue(certificate.subject.contains('rootauthority')); | 61 Expect.isTrue(certificate.subject.contains('rootauthority')); |
| 63 Expect.isTrue(certificate.issuer.contains('rootauthority')); | 62 Expect.isTrue(certificate.issuer.contains('rootauthority')); |
| 64 // Throw exception if one is requested. | 63 // Throw exception if one is requested. |
| 65 if (callbackReturns == 'exception') throw new CustomException(); | 64 if (callbackReturns == 'exception') throw new CustomException(); |
| 66 return callbackReturns; | 65 return callbackReturns; |
| 67 } | 66 } |
| 68 | 67 |
| 69 try { | 68 try { |
| 70 var socket = await SecureSocket.connect( | 69 var socket = await SecureSocket.connect(HOST_NAME, port, |
| 71 HOST_NAME, | 70 context: context, onBadCertificate: badCertificateCallback); |
| 72 port, | 71 Expect.equals('pass', result); // Is rethrown below |
| 73 context: context, | |
| 74 onBadCertificate: badCertificateCallback); | |
| 75 Expect.equals('pass', result); // Is rethrown below | |
| 76 await socket.close(); | 72 await socket.close(); |
| 77 } catch (error) { | 73 } catch (error) { |
| 78 if (error is ExpectException) rethrow; | 74 if (error is ExpectException) rethrow; |
| 79 Expect.notEquals(result, 'pass'); | 75 Expect.notEquals(result, 'pass'); |
| 80 if (result == 'fail') { | 76 if (result == 'fail') { |
| 81 Expect.isTrue(error is HandshakeException || error is ArgumentError); | 77 Expect.isTrue(error is HandshakeException || error is ArgumentError); |
| 82 } else if (result == 'throw') { | 78 } else if (result == 'throw') { |
| 83 Expect.isTrue(error is CustomException); | 79 Expect.isTrue(error is CustomException); |
| 84 } else { | 80 } else { |
| 85 Expect.fail('Unknown expectation $result'); | 81 Expect.fail('Unknown expectation $result'); |
| 86 } | 82 } |
| 87 } | 83 } |
| 88 } | 84 } |
| OLD | NEW |