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