tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_cert_storage_broken_db.js (2350B)


      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/. */
      5 "use strict";
      6 
      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
      9 // it.
     10 
     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);
     16    });
     17  });
     18 }
     19 
     20 async function check_has_prior_revocation_data(certStorage, expectedResult) {
     21  let hasPriorRevocationData = await call_has_prior_data(
     22    certStorage,
     23    Ci.nsICertStorage.DATA_TYPE_REVOCATION
     24  );
     25  Assert.equal(
     26    hasPriorRevocationData,
     27    expectedResult,
     28    `should ${expectedResult ? "have" : "not have"} prior revocation data`
     29  );
     30 }
     31 
     32 async function check_has_prior_cert_data(certStorage, expectedResult) {
     33  let hasPriorCertData = await call_has_prior_data(
     34    certStorage,
     35    Ci.nsICertStorage.DATA_TYPE_CERTIFICATE
     36  );
     37  Assert.equal(
     38    hasPriorCertData,
     39    expectedResult,
     40    `should ${expectedResult ? "have" : "not have"} prior cert data`
     41  );
     42 }
     43 
     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");
     50 
     51  let certStorage = Cc["@mozilla.org/security/certstorage;1"].getService(
     52    Ci.nsICertStorage
     53  );
     54  check_has_prior_revocation_data(certStorage, false);
     55  check_has_prior_cert_data(certStorage, false);
     56 
     57  let result = await new Promise(resolve => {
     58    certStorage.setRevocations([], resolve);
     59  });
     60  Assert.equal(result, Cr.NS_OK, "setRevocations should succeed");
     61 
     62  check_has_prior_revocation_data(certStorage, true);
     63  check_has_prior_cert_data(certStorage, false);
     64 
     65  result = await new Promise(resolve => {
     66    certStorage.addCerts([], resolve);
     67  });
     68  Assert.equal(result, Cr.NS_OK, "addCerts should succeed");
     69 
     70  check_has_prior_revocation_data(certStorage, true);
     71  check_has_prior_cert_data(certStorage, true);
     72 });