| 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 String scriptDirectory = dirname(Platform.script); | |
| 24 String database = join(scriptDirectory, 'pkcert'); | |
| 25 String serverDatabase = join(tempDirectory.path, 'server'); | |
| 26 String clientDatabase = join(tempDirectory.path, 'client'); | |
| 27 new Directory(serverDatabase).createSync(); | |
| 28 new Directory(clientDatabase).createSync(); | |
| 29 | |
| 30 cleanUp() { | |
| 31 if (Platform.isWindows) { | |
| 32 // Delay directory deletion until after this script exits. | |
| 33 // The certificate database files are locked until then. | |
| 34 Process.start('start', // Starts a detatched process. | |
| 35 [Platform.executable, | |
| 36 join(scriptDirectory, 'delete_a_directory_later.dart'), | |
| 37 tempDirectory.path], | |
| 38 runInShell: true); | |
| 39 } else { | |
| 40 tempDirectory.delete(recursive: true); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 Future.wait([ | |
| 45 copyFileToDirectory(join(database, 'cert9.db'), serverDatabase), | |
| 46 copyFileToDirectory(join(database, 'key4.db'), serverDatabase), | |
| 47 copyFileToDirectory(join(database, 'cert9.db'), clientDatabase), | |
| 48 copyFileToDirectory(join(database, 'key4.db'), clientDatabase), | |
| 49 ]).then((_) { | |
| 50 SecureSocket.initialize(database: serverDatabase, | |
| 51 password: 'dartdart', | |
| 52 readOnly: false); | |
| 53 for (var nickname in ['localhost_cert', 'myauthority_cert']) { | |
| 54 Expect.isNotNull(SecureSocket.getCertificate(nickname)); | |
| 55 SecureSocket.removeCertificate(nickname); | |
| 56 Expect.isNull(SecureSocket.getCertificate(nickname)); | |
| 57 } | |
| 58 | |
| 59 var mycerts = new File(join(database, 'localhost.p12')).readAsBytesSync(); | |
| 60 SecureSocket.importCertificatesWithPrivateKeys(mycerts, 'dartdart'); | |
| 61 | |
| 62 checkCertificate('localhost_cert', 'CN=localhost', 'CN=myauthority'); | |
| 63 checkCertificate('myauthority_cert', 'CN=myauthority', 'CN=myauthority'); | |
| 64 | |
| 65 SecureSocket.removeCertificate('myauthority_cert'); | |
| 66 return runServer().then((server) { | |
| 67 var tests = ['certificate_test_client.dart', | |
| 68 'certificate_test_client_database.dart']; | |
| 69 return Future.wait(tests.map((test) => | |
| 70 Process.run(Platform.executable, | |
| 71 ['--checked', | |
| 72 join(scriptDirectory, test), | |
| 73 server.port.toString(), | |
| 74 join(database, 'myauthority.pem'), | |
| 75 clientDatabase]))) | |
| 76 .then(verifyResults) | |
| 77 .whenComplete(server.close); | |
| 78 }); | |
| 79 }) | |
| 80 .whenComplete(cleanUp); | |
| 81 } | |
| 82 | |
| 83 checkCertificate(nickname, subject, issuer) { | |
| 84 var cert = SecureSocket.getCertificate(nickname); | |
| 85 Expect.isTrue(cert is X509Certificate); | |
| 86 Expect.equals(subject, cert.subject); | |
| 87 Expect.equals(issuer, cert.issuer); | |
| 88 } | |
| 89 | |
| 90 Future<SecureServerSocket> runServer() => | |
| 91 SecureServerSocket.bind("localhost", 0, "localhost_cert") | |
| 92 .then((server) => server..listen((socket) => socket.pipe(socket))); | |
| 93 | |
| 94 verifyResults(results) => results.map(verifyResult); | |
| 95 verifyResult(ProcessResult result) { | |
| 96 if (result.exitCode != 0 || !result.stdout.contains('SUCCESS')) { | |
| 97 print("Client failed with exit code ${result.exitCode}"); | |
| 98 print(" stdout (expected \"SUCCESS\\n\"):"); | |
| 99 print(result.stdout); | |
| 100 print(" stderr:"); | |
| 101 print(result.stderr); | |
| 102 Expect.fail("Client failed"); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 Future copyFileToDirectory(String file, String directory) { | |
| 107 switch (Platform.operatingSystem) { | |
| 108 case 'linux': | |
| 109 case 'macos': | |
| 110 return Process.run('cp', [file, directory]); | |
| 111 case 'windows': | |
| 112 return Process.run('cmd.exe', ['/C', 'copy $file $directory']); | |
| 113 default: | |
| 114 Expect.fail('Unknown operating system ${Platform.operatingSystem}'); | |
| 115 } | |
| 116 } | |
| OLD | NEW |