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 // This test verifies that a server certificate can be verified by a client |
| 6 // that loads the certificate authority certificate it depends on at runtime. |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 import "package:path/path.dart"; |
| 10 import "dart:io"; |
| 11 import "dart:async"; |
| 12 |
| 13 Future copyFileToDirectory(String file, String directory) { |
| 14 switch (Platform.operatingSystem) { |
| 15 case 'linux': |
| 16 case 'macos': |
| 17 return Process.run('cp', [file, directory]); |
| 18 case 'windows': |
| 19 return Process.run('cmd.exe', ['/C', 'copy $file $directory']); |
| 20 default: |
| 21 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 22 } |
| 23 } |
| 24 |
| 25 |
| 26 void main() { |
| 27 String scriptDirectory = dirname(Platform.script); |
| 28 Directory tempDirectory = new Directory('').createTempSync(); |
| 29 String testDirectory = tempDirectory.path; |
| 30 copyFileToDirectory(join(scriptDirectory, 'pkcert', 'cert9.db'), |
| 31 testDirectory).then((_) => |
| 32 copyFileToDirectory(join(scriptDirectory, 'pkcert', 'key4.db'), |
| 33 testDirectory)).then((_) { |
| 34 SecureSocket.initialize(database: testDirectory, |
| 35 password: 'dartdart', |
| 36 readOnly: false); |
| 37 |
| 38 SecureSocket.removeCertificate('localhost_cert'); |
| 39 SecureSocket.removeCertificate('myauthority_cert'); |
| 40 // TODO: Check existence of certificates, if we add a getCertificate method. |
| 41 |
| 42 var certificate = join(scriptDirectory, 'pkcert', 'localhost.p12'); |
| 43 var mycert = new File(certificate).readAsBytesSync(); |
| 44 SecureSocket.importPrivateCertificates(mycert, 'dartdart'); |
| 45 SecureSocket.removeCertificate('myauthority_cert'); |
| 46 |
| 47 return runServer(); |
| 48 }).then((SecureServerSocket server) { |
| 49 return Process.run(Platform.executable, |
| 50 ['--checked', |
| 51 join(scriptDirectory, 'certificate_test_client.dart'), |
| 52 server.port.toString(), |
| 53 join(scriptDirectory, 'pkcert', 'myauthority.pem')]); |
| 54 }).then((ProcessResult result) { |
| 55 if (result.exitCode != 0) { |
| 56 print("Client failed with exit code ${result.exitCode}"); |
| 57 print(" stdout:"); |
| 58 print(result.stdout); |
| 59 print(" stderr:"); |
| 60 print(result.stderr); |
| 61 throw new AssertionError(); |
| 62 } |
| 63 }).whenComplete(() { |
| 64 tempDirectory.delete(recursive: true); |
| 65 }); |
| 66 } |
| 67 |
| 68 Future<SecureServerSocket> runServer() => |
| 69 SecureServerSocket.bind("localhost", 0, "localhost_cert") |
| 70 .then((server) => server..listen( |
| 71 (socket) => socket.pipe(socket).then((_) => server.close()))); |
OLD | NEW |