tor-browser

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

test_migration_lz4compression.js (4858B)


      1 "use strict";
      2 
      3 const { SessionWriter } = ChromeUtils.importESModule(
      4  "resource:///modules/sessionstore/SessionWriter.sys.mjs"
      5 );
      6 
      7 // Make sure that we have a profile before initializing SessionFile.
      8 const profd = do_get_profile();
      9 const { SessionFile } = ChromeUtils.importESModule(
     10  "resource:///modules/sessionstore/SessionFile.sys.mjs"
     11 );
     12 const Paths = SessionFile.Paths;
     13 
     14 // We need a XULAppInfo to initialize SessionFile
     15 const { updateAppInfo } = ChromeUtils.importESModule(
     16  "resource://testing-common/AppInfo.sys.mjs"
     17 );
     18 updateAppInfo({
     19  name: "SessionRestoreTest",
     20  ID: "{230de50e-4cd1-11dc-8314-0800200c9a66}",
     21  version: "1",
     22  platformVersion: "",
     23 });
     24 
     25 function promise_check_exist(path, shouldExist) {
     26  return (async function () {
     27    info(
     28      "Ensuring that " + path + (shouldExist ? " exists" : " does not exist")
     29    );
     30    if ((await IOUtils.exists(path)) != shouldExist) {
     31      throw new Error(
     32        "File " + path + " should " + (shouldExist ? "exist" : "not exist")
     33      );
     34    }
     35  })();
     36 }
     37 
     38 function promise_check_contents(path, expect) {
     39  return (async function () {
     40    info("Checking whether " + path + " has the right contents");
     41    let actual = await IOUtils.readJSON(path, {
     42      decompress: true,
     43    });
     44    Assert.deepEqual(
     45      actual,
     46      expect,
     47      `File ${path} contains the expected data.`
     48    );
     49  })();
     50 }
     51 
     52 // Check whether the migration from .js to .jslz4 is correct.
     53 add_task(async function test_migration() {
     54  let source = do_get_file("data/sessionstore_valid.js");
     55  source.copyTo(profd, "sessionstore.js");
     56 
     57  // Read the content of the session store file.
     58  let parsed = await IOUtils.readJSON(Paths.clean.replace("jsonlz4", "js"));
     59 
     60  // Read the session file with .js extension.
     61  let result = await SessionFile.read();
     62 
     63  // Check whether the result is what we wanted.
     64  equal(result.origin, "clean");
     65  equal(result.useOldExtension, true);
     66  Assert.deepEqual(
     67    result.parsed,
     68    parsed,
     69    "result.parsed contains expected data"
     70  );
     71 
     72  // Initiate a write to ensure we write the compressed version.
     73  await SessionFile.write(parsed);
     74  await promise_check_exist(Paths.backups, true);
     75  await promise_check_exist(Paths.clean, false);
     76  await promise_check_exist(Paths.cleanBackup, true);
     77  await promise_check_exist(Paths.recovery, true);
     78  await promise_check_exist(Paths.recoveryBackup, false);
     79  await promise_check_exist(Paths.nextUpgradeBackup, true);
     80  // The deprecated $Path.clean should exist.
     81  await promise_check_exist(Paths.clean.replace("jsonlz4", "js"), true);
     82 
     83  await promise_check_contents(Paths.recovery, parsed);
     84 });
     85 
     86 add_task(async function test_startup_with_compressed_clean() {
     87  let state = { windows: [] };
     88 
     89  // Mare sure we have an empty profile dir.
     90  await SessionFile.wipe();
     91 
     92  // Populate session files to profile dir.
     93  await IOUtils.writeJSON(Paths.clean, state, {
     94    compress: true,
     95  });
     96  await IOUtils.makeDirectory(Paths.backups);
     97  await IOUtils.writeJSON(Paths.cleanBackup, state, {
     98    compress: true,
     99  });
    100 
    101  // Initiate a read.
    102  let result = await SessionFile.read();
    103 
    104  // Make sure we read correct session file and its content.
    105  equal(result.origin, "clean");
    106  equal(result.useOldExtension, false);
    107  Assert.deepEqual(
    108    state,
    109    result.parsed,
    110    "result.parsed contains expected data"
    111  );
    112 });
    113 
    114 add_task(async function test_empty_profile_dir() {
    115  // Make sure that we have empty profile dir.
    116  await SessionFile.wipe();
    117  await promise_check_exist(Paths.backups, false);
    118  await promise_check_exist(Paths.clean, false);
    119  await promise_check_exist(Paths.cleanBackup, false);
    120  await promise_check_exist(Paths.recovery, false);
    121  await promise_check_exist(Paths.recoveryBackup, false);
    122  await promise_check_exist(Paths.nextUpgradeBackup, false);
    123  await promise_check_exist(Paths.backups.replace("jsonlz4", "js"), false);
    124  await promise_check_exist(Paths.clean.replace("jsonlz4", "js"), false);
    125  await promise_check_exist(Paths.cleanBackup.replace("lz4", ""), false);
    126  await promise_check_exist(Paths.recovery.replace("jsonlz4", "js"), false);
    127  await promise_check_exist(
    128    Paths.recoveryBackup.replace("jsonlz4", "js"),
    129    false
    130  );
    131  await promise_check_exist(
    132    Paths.nextUpgradeBackup.replace("jsonlz4", "js"),
    133    false
    134  );
    135 
    136  // Initiate a read and make sure that we are in empty state.
    137  let result = await SessionFile.read();
    138  equal(result.origin, "empty");
    139  equal(result.noFilesFound, true);
    140 
    141  // Create a state to store.
    142  let state = { windows: [] };
    143  await SessionWriter.write(state, { isFinalWrite: true });
    144 
    145  // Check session files are created, but not deprecated ones.
    146  await promise_check_exist(Paths.clean, true);
    147  await promise_check_exist(Paths.clean.replace("jsonlz4", "js"), false);
    148 
    149  // Check session file' content is correct.
    150  await promise_check_contents(Paths.clean, state);
    151 });