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 the SecureSocket functions addCertificate, |
| 6 // importCertificatesWithPrivateKeys, changeTrust, getCertificate, and |
| 7 // removeCertificate. |
| 8 |
| 9 // It loads a copy of the test certificate database, |
| 10 // removes all certificates and keys, then imports the certificates and keys |
| 11 // again. Then it runs a secure server, using the user certificate (a |
| 12 // certificate with private key), and starts client processes that use |
| 13 // addCertificate to trust the certificate that signed the server's certificate. |
| 14 // The clients then test that they can successfully connect to the server. |
| 15 |
| 16 import "package:expect/expect.dart"; |
| 17 import "package:path/path.dart"; |
| 18 import "dart:io"; |
| 19 import "dart:async"; |
| 20 |
| 21 void main() { |
| 22 Directory tempDirectory = new Directory('').createTempSync(); |
| 23 cleanUp() => tempDirectory.delete(recursive: true); |
| 24 String scriptDirectory = dirname(Platform.script); |
| 25 String database = join(scriptDirectory, 'pkcert'); |
| 26 String serverDatabase = join(tempDirectory.path, 'server'); |
| 27 String clientDatabase = join(tempDirectory.path, 'client'); |
| 28 new Directory(serverDatabase).createSync(); |
| 29 new Directory(clientDatabase).createSync(); |
| 30 |
| 31 Future.wait([ |
| 32 copyFileToDirectory(join(database, 'cert9.db'), serverDatabase), |
| 33 copyFileToDirectory(join(database, 'key4.db'), serverDatabase), |
| 34 copyFileToDirectory(join(database, 'cert9.db'), clientDatabase), |
| 35 copyFileToDirectory(join(database, 'key4.db'), clientDatabase), |
| 36 ]).then((_) { |
| 37 SecureSocket.initialize(database: serverDatabase, |
| 38 password: 'dartdart', |
| 39 readOnly: false); |
| 40 for (var nickname in ['localhost_cert', 'myauthority_cert']) { |
| 41 Expect.isNotNull(SecureSocket.getCertificate(nickname)); |
| 42 SecureSocket.removeCertificate(nickname); |
| 43 Expect.isNull(SecureSocket.getCertificate(nickname)); |
| 44 } |
| 45 |
| 46 var mycerts = new File(join(database, 'localhost.p12')).readAsBytesSync(); |
| 47 SecureSocket.importCertificatesWithPrivateKeys(mycerts, 'dartdart'); |
| 48 |
| 49 checkCertificate('localhost_cert', 'CN=localhost', 'CN=myauthority'); |
| 50 checkCertificate('myauthority_cert', 'CN=myauthority', 'CN=myauthority'); |
| 51 |
| 52 SecureSocket.removeCertificate('myauthority_cert'); |
| 53 return runServer().then((server) { |
| 54 var tests = ['certificate_test_client.dart', |
| 55 'certificate_test_client_database.dart']; |
| 56 return Future.wait(tests.map((test) => |
| 57 Process.run(Platform.executable, |
| 58 ['--checked', |
| 59 join(scriptDirectory, test), |
| 60 server.port.toString(), |
| 61 join(database, 'myauthority.pem'), |
| 62 clientDatabase]))) |
| 63 .then(verifyResults) |
| 64 .whenComplete(server.close); |
| 65 }); |
| 66 }) |
| 67 .whenComplete(cleanUp); |
| 68 } |
| 69 |
| 70 checkCertificate(nickname, subject, issuer) { |
| 71 var cert = SecureSocket.getCertificate(nickname); |
| 72 Expect.isTrue(cert is X509Certificate); |
| 73 Expect.equals(subject, cert.subject); |
| 74 Expect.equals(issuer, cert.issuer); |
| 75 } |
| 76 |
| 77 Future<SecureServerSocket> runServer() => |
| 78 SecureServerSocket.bind("localhost", 0, "localhost_cert") |
| 79 .then((server) => server..listen((socket) => socket.pipe(socket))); |
| 80 |
| 81 verifyResults(results) => results.map(verifyResult); |
| 82 verifyResult(ProcessResult result) { |
| 83 if (result.exitCode != 0 || "SUCCESS\n" != result.stdout) { |
| 84 print("Client failed with exit code ${result.exitCode}"); |
| 85 print(" stdout (expected \"SUCCESS\\n\"):"); |
| 86 print(result.stdout); |
| 87 print(" stderr:"); |
| 88 print(result.stderr); |
| 89 Expect.fail("Client failed"); |
| 90 } |
| 91 } |
| 92 |
| 93 Future copyFileToDirectory(String file, String directory) { |
| 94 switch (Platform.operatingSystem) { |
| 95 case 'linux': |
| 96 case 'macos': |
| 97 return Process.run('cp', [file, directory]); |
| 98 case 'windows': |
| 99 return Process.run('cmd.exe', ['/C', 'copy $file $directory']); |
| 100 default: |
| 101 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); |
| 102 } |
| 103 } |
OLD | NEW |