1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/publicdomain/zero/1.0/
6 // Tests the various nsIX509CertDB import methods.
10 const gCertDB = Cc["@mozilla.org/security/x509certdb;1"].getService(
14 const CA_CERT_COMMON_NAME = "importedCA";
15 const TEST_EMAIL_ADDRESS = "test@example.com";
17 let gCACertImportDialogCount = 0;
19 // Mock implementation of nsICertificateDialogs.
20 const gCertificateDialogs = {
21 confirmDownloadCACert: (ctx, cert, trust) => {
22 gCACertImportDialogCount++;
26 "CA cert to import should have the correct CN"
28 trust.value = Ci.nsIX509CertDB.TRUSTED_EMAIL;
31 setPKCS12FilePassword: () => {
32 // This is only relevant to exporting.
33 ok(false, "setPKCS12FilePassword() should not have been called");
35 getPKCS12FilePassword: () => {
36 // We don't test anything that calls this method yet.
37 ok(false, "getPKCS12FilePassword() should not have been called");
40 QueryInterface: ChromeUtils.generateQI(["nsICertificateDialogs"]),
43 // Implements nsIInterfaceRequestor. Mostly serves to mock nsIPrompt.
44 const gInterfaceRequestor = {
45 alert: (title, text) => {
46 // We don't test anything that calls this method yet.
47 ok(false, `alert() should not have been called: ${text}`);
50 getInterface: iid => {
51 if (iid.equals(Ci.nsIPrompt)) {
55 throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
59 function getCertAsByteArray(certPath) {
60 let certFile = do_get_file(certPath, false);
61 let certBytes = readFile(certFile);
64 for (let i = 0; i < certBytes.length; i++) {
65 byteArray.push(certBytes.charCodeAt(i));
71 function commonFindCertBy(propertyName, value) {
72 for (let cert of gCertDB.getCerts()) {
73 if (cert[propertyName] == value) {
80 function findCertByCommonName(commonName) {
81 return commonFindCertBy("commonName", commonName);
84 function findCertByEmailAddress(emailAddress) {
85 return commonFindCertBy("emailAddress", emailAddress);
88 function testImportCACert() {
89 // Sanity check the CA cert is missing.
91 findCertByCommonName(CA_CERT_COMMON_NAME),
93 "CA cert should not be in the database before import"
96 // Import and check for success.
97 let caArray = getCertAsByteArray("test_certDB_import/importedCA.pem");
98 gCertDB.importCertificates(
101 Ci.nsIX509Cert.CA_CERT,
105 gCACertImportDialogCount,
107 "Confirmation dialog for the CA cert should only be shown once"
110 let caCert = findCertByCommonName(CA_CERT_COMMON_NAME);
111 notEqual(caCert, null, "CA cert should now be found in the database");
113 gCertDB.isCertTrusted(
115 Ci.nsIX509Cert.CA_CERT,
116 Ci.nsIX509CertDB.TRUSTED_EMAIL
118 "CA cert should be trusted for e-mail"
122 function testImportEmptyCertPackage() {
123 // Because this is an empty cert package, nothing will be imported. We know it succeeded if no errors are thrown.
125 0x30, 0x0f, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x02,
126 0x05, 0xa0, 0x02, 0x30, 0x00,
128 gCertDB.importCertificates(
131 Ci.nsIX509Cert.CA_CERT,
136 function testImportEmptyUserCert() {
137 // Because this is an empty cert package, nothing will be imported. We know it succeeded if no errors are thrown.
139 0x30, 0x0f, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x02,
140 0x05, 0xa0, 0x02, 0x30, 0x00,
142 gCertDB.importUserCertificate(
149 function run_test() {
150 let certificateDialogsCID = MockRegistrar.register(
151 "@mozilla.org/nsCertificateDialogs;1",
154 registerCleanupFunction(() => {
155 MockRegistrar.unregister(certificateDialogsCID);
158 // Sanity check the e-mail cert is missing.
160 findCertByEmailAddress(TEST_EMAIL_ADDRESS),
162 "E-mail cert should not be in the database before import"
165 // Import the CA cert so that the e-mail import succeeds.
167 testImportEmptyCertPackage();
168 testImportEmptyUserCert();
170 // Import the e-mail cert and check for success.
171 let emailArray = getCertAsByteArray("test_certDB_import/emailEE.pem");
172 gCertDB.importEmailCertificate(
177 let emailCert = findCertByEmailAddress(TEST_EMAIL_ADDRESS);
178 notEqual(emailCert, null, "E-mail cert should now be found in the database");
179 let bundle = Services.strings.createBundle(
180 "chrome://pipnss/locale/pipnss.properties"
184 bundle.GetStringFromName("PrivateTokenDescription"),
185 "cert's tokenName should be the expected localized value"