tor-browser

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

test_ResourceFailures.js (3445B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  BackupError: "resource:///modules/backup/BackupError.mjs",
      8  ERRORS: "chrome://browser/content/backup/backup-constants.mjs",
      9  AppConstants: "resource://gre/modules/AppConstants.sys.mjs",
     10 });
     11 
     12 add_setup(function () {
     13  setupProfile();
     14 });
     15 
     16 /**
     17 * Test's the case where a resource fails to recover successfully. We expect
     18 * the complete recover process to error out.
     19 */
     20 add_task(async function testResourceFailure() {
     21  let sandbox = sinon.createSandbox();
     22  registerCleanupFunction(() => {
     23    sandbox.restore();
     24  });
     25 
     26  let testBackupPath = await IOUtils.createUniqueDirectory(
     27    PathUtils.tempDir,
     28    "checkForErrorsTestBackup"
     29  );
     30 
     31  let fake1ManifestEntry = { fake1: "hello from 1" };
     32  sandbox
     33    .stub(FakeBackupResource1.prototype, "backup")
     34    .resolves(fake1ManifestEntry);
     35  sandbox.stub(FakeBackupResource1.prototype, "recover").resolves();
     36 
     37  let fake2ManifestEntry = { fake1: "hello from 2" };
     38  sandbox
     39    .stub(FakeBackupResource2.prototype, "backup")
     40    .resolves(fake2ManifestEntry);
     41  sandbox
     42    .stub(FakeBackupResource2.prototype, "recover")
     43    .rejects(new BackupError("recovery failed", ERRORS.RECOVERY_FAILED));
     44 
     45  let fake3ManifestEntry = { fake1: "hello from 3" };
     46  sandbox
     47    .stub(FakeBackupResource3.prototype, "backup")
     48    .resolves(fake3ManifestEntry);
     49  sandbox.stub(FakeBackupResource3.prototype, "recover").resolves();
     50 
     51  let bs = new BackupService({
     52    FakeBackupResource1,
     53    FakeBackupResource2,
     54    FakeBackupResource3,
     55  });
     56 
     57  let { manifest, archivePath: backupFilePath } = await bs.createBackup({
     58    profilePath: testBackupPath,
     59  });
     60  Assert.ok(await IOUtils.exists(backupFilePath), "The backup file exists");
     61 
     62  let archiveDateSuffix = bs.generateArchiveDateSuffix(
     63    new Date(manifest.meta.date)
     64  );
     65 
     66  // We also expect the HTML file to have been written to the folder pointed
     67  // at by browser.backups.location, within backupDirPath folder.
     68  const EXPECTED_ARCHIVE_PATH = PathUtils.join(
     69    bs.state.backupDirPath,
     70    `${BackupService.BACKUP_FILE_NAME}_${manifest.meta.profileName}_${archiveDateSuffix}.html`
     71  );
     72  Assert.ok(
     73    await IOUtils.exists(EXPECTED_ARCHIVE_PATH),
     74    "Single-file backup archive was written."
     75  );
     76  Assert.equal(
     77    backupFilePath,
     78    EXPECTED_ARCHIVE_PATH,
     79    "Backup was written to the configured destination folder"
     80  );
     81  Assert.deepEqual(
     82    Object.keys(manifest.resources).sort(),
     83    ["fake1", "fake2", "fake3"],
     84    "Manifest contains all expected BackupResource keys"
     85  );
     86 
     87  let recoveredProfilePath = await IOUtils.createUniqueDirectory(
     88    PathUtils.tempDir,
     89    "createBackupTestRecoveredProfile"
     90  );
     91 
     92  await Assert.rejects(
     93    bs.recoverFromBackupArchive(
     94      backupFilePath,
     95      null,
     96      false,
     97      testBackupPath,
     98      recoveredProfilePath
     99    ),
    100    err => err.cause == ERRORS.RECOVERY_FAILED
    101  );
    102 
    103  // Since we fail on backupResource2, backupResource1 should never be called
    104  sinon.assert.notCalled(FakeBackupResource1.prototype.recover);
    105 
    106  // Ideally, we should be removing any left over data when we fail.
    107  // The current behavior does not do this, so let's clear the paths
    108  // manually in the test.
    109  await maybeRemovePath(backupFilePath);
    110  await maybeRemovePath(testBackupPath);
    111  await maybeRemovePath(recoveredProfilePath);
    112 });