tor-browser

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

test_BookmarksBackupResource.js (4715B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { BookmarksBackupResource } = ChromeUtils.importESModule(
      7  "resource:///modules/backup/BookmarksBackupResource.sys.mjs"
      8 );
      9 const { BookmarkJSONUtils } = ChromeUtils.importESModule(
     10  "resource://gre/modules/BookmarkJSONUtils.sys.mjs"
     11 );
     12 
     13 /**
     14 * Test that the backup method correctly creates a compressed bookmarks JSON file.
     15 */
     16 add_task(async function test_backup() {
     17  let sandbox = sinon.createSandbox();
     18 
     19  let bookmarksBackupResource = new BookmarksBackupResource();
     20  let sourcePath = await IOUtils.createUniqueDirectory(
     21    PathUtils.tempDir,
     22    "BookmarksBackupResource-source-test"
     23  );
     24  let stagingPath = await IOUtils.createUniqueDirectory(
     25    PathUtils.tempDir,
     26    "BookmarksBackupResource-staging-test"
     27  );
     28 
     29  let exportStub = sandbox.stub(BookmarkJSONUtils, "exportToFile").resolves();
     30 
     31  let manifestEntry = await bookmarksBackupResource.backup(
     32    stagingPath,
     33    sourcePath
     34  );
     35  Assert.equal(
     36    manifestEntry,
     37    null,
     38    "BookmarksBackupResource.backup should return null as its ManifestEntry"
     39  );
     40 
     41  Assert.ok(
     42    exportStub.calledOnce,
     43    "BookmarkJSONUtils.exportToFile should have been called once"
     44  );
     45  Assert.ok(
     46    exportStub.calledWith(PathUtils.join(stagingPath, "bookmarks.jsonlz4"), {
     47      compress: true,
     48    }),
     49    "BookmarkJSONUtils.exportToFile should have been called with the correct arguments"
     50  );
     51 
     52  await maybeRemovePath(stagingPath);
     53  await maybeRemovePath(sourcePath);
     54 
     55  sandbox.restore();
     56 });
     57 
     58 /**
     59 * Test that the recover method correctly returns the path to the bookmarks backup file.
     60 */
     61 add_task(async function test_recover() {
     62  let bookmarksBackupResource = new BookmarksBackupResource();
     63  let recoveryPath = await IOUtils.createUniqueDirectory(
     64    PathUtils.tempDir,
     65    "BookmarksBackupResource-recovery-test"
     66  );
     67  let destProfilePath = await IOUtils.createUniqueDirectory(
     68    PathUtils.tempDir,
     69    "BookmarksBackupResource-test-profile"
     70  );
     71 
     72  await createTestFiles(recoveryPath, [{ path: "bookmarks.jsonlz4" }]);
     73 
     74  let postRecoveryEntry = await bookmarksBackupResource.recover(
     75    null,
     76    recoveryPath,
     77    destProfilePath
     78  );
     79 
     80  let expectedBookmarksPath = PathUtils.join(recoveryPath, "bookmarks.jsonlz4");
     81 
     82  Assert.deepEqual(
     83    postRecoveryEntry,
     84    { bookmarksBackupPath: expectedBookmarksPath },
     85    "BookmarksBackupResource.recover should return the path to the bookmarks backup file"
     86  );
     87 
     88  // The bookmarks file should not be copied to the destination profile during recovery,
     89  // it will be imported in postRecovery
     90  Assert.ok(
     91    !(await IOUtils.exists(
     92      PathUtils.join(destProfilePath, "bookmarks.jsonlz4")
     93    )),
     94    "bookmarks.jsonlz4 should not exist in the destination profile yet"
     95  );
     96 
     97  await maybeRemovePath(recoveryPath);
     98  await maybeRemovePath(destProfilePath);
     99 });
    100 
    101 /**
    102 * Test that the postRecovery method correctly imports bookmarks from the backup file.
    103 */
    104 add_task(async function test_postRecovery() {
    105  let sandbox = sinon.createSandbox();
    106 
    107  let bookmarksBackupResource = new BookmarksBackupResource();
    108  let recoveryPath = await IOUtils.createUniqueDirectory(
    109    PathUtils.tempDir,
    110    "BookmarksBackupResource-recovery-test"
    111  );
    112 
    113  await createTestFiles(recoveryPath, [{ path: "bookmarks.jsonlz4" }]);
    114 
    115  let bookmarksBackupPath = PathUtils.join(recoveryPath, "bookmarks.jsonlz4");
    116  let importStub = sandbox
    117    .stub(BookmarkJSONUtils, "importFromFile")
    118    .resolves(true);
    119 
    120  await bookmarksBackupResource.postRecovery({ bookmarksBackupPath });
    121 
    122  Assert.ok(
    123    importStub.calledOnce,
    124    "BookmarkJSONUtils.importFromFile should have been called once"
    125  );
    126  Assert.ok(
    127    importStub.calledWith(bookmarksBackupPath, { replace: true }),
    128    "BookmarkJSONUtils.importFromFile should have been called with the correct arguments"
    129  );
    130 
    131  await maybeRemovePath(recoveryPath);
    132 
    133  sandbox.restore();
    134 });
    135 
    136 /**
    137 * Test that postRecovery handles missing bookmarksBackupPath gracefully.
    138 */
    139 add_task(async function test_postRecovery_no_path() {
    140  let sandbox = sinon.createSandbox();
    141 
    142  let bookmarksBackupResource = new BookmarksBackupResource();
    143  let importStub = sandbox
    144    .stub(BookmarkJSONUtils, "importFromFile")
    145    .resolves(true);
    146 
    147  await bookmarksBackupResource.postRecovery(null);
    148 
    149  Assert.ok(
    150    importStub.notCalled,
    151    "BookmarkJSONUtils.importFromFile should not have been called with null postRecoveryEntry"
    152  );
    153 
    154  await bookmarksBackupResource.postRecovery({});
    155 
    156  Assert.ok(
    157    importStub.notCalled,
    158    "BookmarkJSONUtils.importFromFile should not have been called with empty postRecoveryEntry"
    159  );
    160 
    161  sandbox.restore();
    162 });