tor-browser

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

test_SiteSettingsBackupResource.js (7458B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { SiteSettingsBackupResource } = ChromeUtils.importESModule(
      7  "resource:///modules/backup/SiteSettingsBackupResource.sys.mjs"
      8 );
      9 
     10 /**
     11 * Tests that we can measure the Site Settings databases in a profile directory.
     12 */
     13 add_task(async function test_measure() {
     14  const EXPECTED_PERMISSIONS_DB_SIZE = 500;
     15  const EXPECTED_CONTENT_PREFS_DB_SIZE = 500;
     16 
     17  Services.fog.testResetFOG();
     18 
     19  let tempDir = PathUtils.tempDir;
     20  let tempPermissionsDBPath = PathUtils.join(tempDir, "permissions.sqlite");
     21  let tempContentPrefsDBPath = PathUtils.join(tempDir, "content-prefs.sqlite");
     22 
     23  await createKilobyteSizedFile(
     24    tempPermissionsDBPath,
     25    EXPECTED_PERMISSIONS_DB_SIZE
     26  );
     27  await createKilobyteSizedFile(
     28    tempContentPrefsDBPath,
     29    EXPECTED_CONTENT_PREFS_DB_SIZE
     30  );
     31 
     32  let siteSettingsBackupResource = new SiteSettingsBackupResource();
     33  await siteSettingsBackupResource.measure(tempDir);
     34 
     35  await IOUtils.remove(tempPermissionsDBPath);
     36  await IOUtils.remove(tempContentPrefsDBPath);
     37 });
     38 
     39 /**
     40 * Test that the backup method correctly copies items from the profile directory
     41 * into the staging directory.
     42 */
     43 add_task(async function test_backup() {
     44  let sandbox = sinon.createSandbox();
     45 
     46  let siteSettingsBackupResource = new SiteSettingsBackupResource();
     47  let sourcePath = await IOUtils.createUniqueDirectory(
     48    PathUtils.tempDir,
     49    "SiteSettingsBackupResource-source-test"
     50  );
     51  let stagingPath = await IOUtils.createUniqueDirectory(
     52    PathUtils.tempDir,
     53    "SiteSettingsBackupResource-staging-test"
     54  );
     55 
     56  await createTestFiles(sourcePath, [
     57    { path: "permissions.sqlite" },
     58    { path: "content-prefs.sqlite" },
     59  ]);
     60 
     61  let fakeConnection = {
     62    backup: sandbox.stub().resolves(true),
     63    close: sandbox.stub().resolves(true),
     64  };
     65  sandbox.stub(Sqlite, "openConnection").returns(fakeConnection);
     66 
     67  let manifestEntry = await siteSettingsBackupResource.backup(
     68    stagingPath,
     69    sourcePath
     70  );
     71  Assert.equal(
     72    manifestEntry,
     73    null,
     74    "SiteSettingsBackupResource.backup should return null as its ManifestEntry"
     75  );
     76 
     77  Assert.ok(
     78    fakeConnection.backup.calledTwice,
     79    "Called backup the expected number of times for all connections"
     80  );
     81  Assert.ok(
     82    fakeConnection.backup.calledWith(
     83      PathUtils.join(stagingPath, "permissions.sqlite")
     84    ),
     85    "Called backup on the permissions.sqlite Sqlite connection"
     86  );
     87  Assert.ok(
     88    fakeConnection.backup.calledWith(
     89      PathUtils.join(stagingPath, "content-prefs.sqlite")
     90    ),
     91    "Called backup on the content-prefs.sqlite Sqlite connection"
     92  );
     93 
     94  await maybeRemovePath(stagingPath);
     95  await maybeRemovePath(sourcePath);
     96 
     97  sandbox.restore();
     98 });
     99 
    100 /**
    101 * Test that the recover method correctly copies items from the recovery
    102 * directory into the destination profile directory.
    103 */
    104 add_task(async function test_recover() {
    105  let siteSettingsBackupResource = new SiteSettingsBackupResource();
    106  let recoveryPath = await IOUtils.createUniqueDirectory(
    107    PathUtils.tempDir,
    108    "SiteSettingsBackupResource-recovery-test"
    109  );
    110  let destProfilePath = await IOUtils.createUniqueDirectory(
    111    PathUtils.tempDir,
    112    "SiteSettingsBackupResource-test-profile"
    113  );
    114 
    115  const simpleCopyFiles = [
    116    { path: "permissions.sqlite" },
    117    { path: "content-prefs.sqlite" },
    118  ];
    119  await createTestFiles(recoveryPath, simpleCopyFiles);
    120 
    121  let postRecoveryEntry = await siteSettingsBackupResource.recover(
    122    null,
    123    recoveryPath,
    124    destProfilePath
    125  );
    126  Assert.equal(
    127    postRecoveryEntry,
    128    null,
    129    "SiteSettingsBackupResource.recover should return null as its post recovery entry"
    130  );
    131 
    132  await assertFilesExist(destProfilePath, simpleCopyFiles);
    133 
    134  await maybeRemovePath(recoveryPath);
    135  await maybeRemovePath(destProfilePath);
    136 });
    137 
    138 /**
    139 * Tests that the backup method does not copy the permissions or content prefs
    140 * databases if the browser is configured to not save history - either while
    141 * running, or to clear it at shutdown.
    142 */
    143 add_task(async function test_backup_no_saved_history() {
    144  let siteSettingsBackupResource = new SiteSettingsBackupResource();
    145  let sourcePath = await IOUtils.createUniqueDirectory(
    146    PathUtils.tempDir,
    147    "SiteSettingsBackupResource-source-test"
    148  );
    149  let stagingPath = await IOUtils.createUniqueDirectory(
    150    PathUtils.tempDir,
    151    "SiteSettingsBackupResource-staging-test"
    152  );
    153 
    154  let sandbox = sinon.createSandbox();
    155  let fakeConnection = {
    156    backup: sandbox.stub().resolves(true),
    157    close: sandbox.stub().resolves(true),
    158  };
    159  sandbox.stub(Sqlite, "openConnection").returns(fakeConnection);
    160 
    161  Services.prefs.setBoolPref(SANITIZE_ON_SHUTDOWN_PREF, true);
    162  Services.prefs.setBoolPref(SITE_SETTINGS_CLEARED_ON_SHUTDOWN_PREF, true);
    163 
    164  Assert.ok(
    165    !SiteSettingsBackupResource.canBackupResource,
    166    "Cannot backup site settings since they are being cleared on shutdown"
    167  );
    168 
    169  let manifestEntry = await siteSettingsBackupResource.backup(
    170    stagingPath,
    171    sourcePath
    172  );
    173  Assert.equal(
    174    manifestEntry,
    175    null,
    176    "SiteSettingsBackupResource.backup should return null as its ManifestEntry"
    177  );
    178 
    179  Assert.ok(
    180    fakeConnection.backup.notCalled,
    181    "No sqlite connections should have been made with sanitize shutdown enabled"
    182  );
    183 
    184  Services.prefs.clearUserPref(SANITIZE_ON_SHUTDOWN_PREF);
    185  Services.prefs.clearUserPref(SITE_SETTINGS_CLEARED_ON_SHUTDOWN_PREF);
    186 
    187  Assert.ok(
    188    SiteSettingsBackupResource.canBackupResource,
    189    "We should be allowed to backup this resource now"
    190  );
    191 
    192  fakeConnection.backup.resetHistory();
    193  manifestEntry = await siteSettingsBackupResource.backup(
    194    stagingPath,
    195    sourcePath
    196  );
    197  Assert.equal(
    198    manifestEntry,
    199    null,
    200    "Should have gotten back a null ManifestEntry"
    201  );
    202 
    203  Assert.ok(
    204    fakeConnection.backup.notCalled,
    205    "No files to backup, so no sqlite connections should have been made"
    206  );
    207 
    208  await maybeRemovePath(stagingPath);
    209  await maybeRemovePath(sourcePath);
    210 
    211  sandbox.restore();
    212 });
    213 
    214 /**
    215 * Tests the canBackupResource method with various pref configurations.
    216 */
    217 add_task(async function test_canBackupResource() {
    218  Assert.ok(
    219    SiteSettingsBackupResource.canBackupResource,
    220    "Should be able to backup by default"
    221  );
    222 
    223  Services.prefs.setBoolPref(SANITIZE_ON_SHUTDOWN_PREF, true);
    224  Services.prefs.setBoolPref(SITE_SETTINGS_CLEARED_ON_SHUTDOWN_PREF, true);
    225  Assert.ok(
    226    !SiteSettingsBackupResource.canBackupResource,
    227    "Cannot backup when sanitizeOnShutdown and site settings cleared on shutdown (v2) are enabled"
    228  );
    229  Services.prefs.clearUserPref(SANITIZE_ON_SHUTDOWN_PREF);
    230  Services.prefs.clearUserPref(SITE_SETTINGS_CLEARED_ON_SHUTDOWN_PREF);
    231 
    232  Assert.ok(
    233    SiteSettingsBackupResource.canBackupResource,
    234    "Should be able to backup after clearing prefs"
    235  );
    236 
    237  Services.prefs.setBoolPref(USE_OLD_CLEAR_HISTORY_DIALOG_PREF, true);
    238  Services.prefs.setBoolPref(SANITIZE_ON_SHUTDOWN_PREF, true);
    239  Services.prefs.setBoolPref(SITE_SETTINGS_CLEARED_ON_SHUTDOWN_OLD_PREF, true);
    240  Assert.ok(
    241    !SiteSettingsBackupResource.canBackupResource,
    242    "Cannot backup when sanitizeOnShutdown and site settings cleared on shutdown (old) are enabled"
    243  );
    244 
    245  Services.prefs.clearUserPref(USE_OLD_CLEAR_HISTORY_DIALOG_PREF);
    246  Services.prefs.clearUserPref(SANITIZE_ON_SHUTDOWN_PREF);
    247  Services.prefs.clearUserPref(SITE_SETTINGS_CLEARED_ON_SHUTDOWN_OLD_PREF);
    248 });