1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // This file tests cert_storage's automatic database recreation mechanism. If
8 // opening the database for the first time fails, cert_storage will re-create
11 function call_has_prior_data(certStorage, type) {
12 return new Promise(resolve => {
13 certStorage.hasPriorData(type, (rv, hasPriorData) => {
14 Assert.equal(rv, Cr.NS_OK, "hasPriorData should succeed");
15 resolve(hasPriorData);
20 async function check_has_prior_revocation_data(certStorage, expectedResult) {
21 let hasPriorRevocationData = await call_has_prior_data(
23 Ci.nsICertStorage.DATA_TYPE_REVOCATION
26 hasPriorRevocationData,
28 `should ${expectedResult ? "have" : "not have"} prior revocation data`
32 async function check_has_prior_cert_data(certStorage, expectedResult) {
33 let hasPriorCertData = await call_has_prior_data(
35 Ci.nsICertStorage.DATA_TYPE_CERTIFICATE
40 `should ${expectedResult ? "have" : "not have"} prior cert data`
44 add_task(async function () {
45 // Create an invalid database.
46 let fileToCopy = do_get_file("test_cert_storage_broken_db.js");
47 let dbDirectory = do_get_profile();
48 dbDirectory.append("security_state");
49 fileToCopy.copyTo(dbDirectory, "data.mdb");
51 let certStorage = Cc["@mozilla.org/security/certstorage;1"].getService(
54 check_has_prior_revocation_data(certStorage, false);
55 check_has_prior_cert_data(certStorage, false);
57 let result = await new Promise(resolve => {
58 certStorage.setRevocations([], resolve);
60 Assert.equal(result, Cr.NS_OK, "setRevocations should succeed");
62 check_has_prior_revocation_data(certStorage, true);
63 check_has_prior_cert_data(certStorage, false);
65 result = await new Promise(resolve => {
66 certStorage.addCerts([], resolve);
68 Assert.equal(result, Cr.NS_OK, "addCerts should succeed");
70 check_has_prior_revocation_data(certStorage, true);
71 check_has_prior_cert_data(certStorage, true);