tor-browser

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

test_BackupService_wrongPassword.js (2987B)


      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  ERRORS: "chrome://browser/content/backup/backup-constants.mjs",
      8 });
      9 
     10 let bs;
     11 const correctPassword = "correcthorsebatterystaple";
     12 const incorrectPassword = "Tr0ub4dor&3";
     13 let testBackupDirPath;
     14 let testBackupPath;
     15 
     16 add_setup(async function () {
     17  setupProfile();
     18 
     19  bs = new BackupService({ FakeBackupResource1 });
     20  let sandbox = sinon.createSandbox();
     21  let fakeManifestEntry = { fake: "test" };
     22  sandbox
     23    .stub(FakeBackupResource1.prototype, "backup")
     24    .resolves(fakeManifestEntry);
     25  sandbox.stub(FakeBackupResource1.prototype, "recover").resolves();
     26 
     27  testBackupDirPath = await IOUtils.createUniqueDirectory(
     28    PathUtils.tempDir,
     29    "wrongPasswordTestBackup"
     30  );
     31  bs = new BackupService({ FakeBackupResource1 });
     32  await bs.enableEncryption(correctPassword);
     33  testBackupPath = (await bs.createBackup({ profilePath: testBackupDirPath }))
     34    .archivePath;
     35 
     36  registerCleanupFunction(async () => {
     37    sandbox.restore();
     38    bs = null;
     39 
     40    await IOUtils.remove(testBackupDirPath, { recursive: true });
     41  });
     42 });
     43 
     44 /**
     45 * Tests the case where the wrong password is given when trying to restore from
     46 * a backup.
     47 *
     48 * @param  {string|null} passwordToUse
     49 *         The password to decrypt with, or `null` to specify no password.
     50 */
     51 async function testWrongPassword(passwordToUse) {
     52  Services.fog.testResetFOG();
     53 
     54  Assert.ok(await IOUtils.exists(testBackupPath), "The backup file exists");
     55 
     56  let recoveredProfilePath = await IOUtils.createUniqueDirectory(
     57    PathUtils.tempDir,
     58    "wrongPasswordTestRecoveredProfile"
     59  );
     60  registerCleanupFunction(async () => {
     61    await IOUtils.remove(recoveredProfilePath, { recursive: true });
     62  });
     63 
     64  await bs.getBackupFileInfo(testBackupPath);
     65  const restoreID = bs.state.restoreID;
     66 
     67  await Assert.rejects(
     68    bs.recoverFromBackupArchive(
     69      testBackupPath,
     70      passwordToUse,
     71      false,
     72      testBackupDirPath,
     73      recoveredProfilePath
     74    ),
     75    err => err.cause == ERRORS.UNAUTHORIZED
     76  );
     77 
     78  let events = Glean.browserBackup.restoreStarted.testGetValue();
     79  Assert.equal(
     80    events.length,
     81    1,
     82    "Should be a single restore started event after we start restoring a profile"
     83  );
     84  Assert.deepEqual(
     85    events[0].extra,
     86    { restore_id: restoreID },
     87    "Restore event should have the right data"
     88  );
     89 
     90  events = Glean.browserBackup.restoreFailed.testGetValue();
     91  Assert.equal(
     92    events.length,
     93    1,
     94    "Should be a single restore failed event after we fail to restore a profile"
     95  );
     96  Assert.deepEqual(
     97    events[0].extra,
     98    { restore_id: restoreID, error_type: "UNAUTHORIZED" },
     99    "Restore failure event should have the right data"
    100  );
    101 }
    102 
    103 add_task(async function test_wrong_password() {
    104  await testWrongPassword(incorrectPassword);
    105 });
    106 
    107 add_task(async function test_no_password() {
    108  await testWrongPassword(null);
    109 });