summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/unit/test_migration_lz4compression.js
blob: 4d9b700d8bbdce81f1efee9c83b4d073187ecbc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";

const { SessionWriter } = ChromeUtils.importESModule(
  "resource:///modules/sessionstore/SessionWriter.sys.mjs"
);

// Make sure that we have a profile before initializing SessionFile.
const profd = do_get_profile();
const { SessionFile } = ChromeUtils.importESModule(
  "resource:///modules/sessionstore/SessionFile.sys.mjs"
);
const Paths = SessionFile.Paths;

// We need a XULAppInfo to initialize SessionFile
const { updateAppInfo } = ChromeUtils.importESModule(
  "resource://testing-common/AppInfo.sys.mjs"
);
updateAppInfo({
  name: "SessionRestoreTest",
  ID: "{230de50e-4cd1-11dc-8314-0800200c9a66}",
  version: "1",
  platformVersion: "",
});

function promise_check_exist(path, shouldExist) {
  return (async function () {
    info(
      "Ensuring that " + path + (shouldExist ? " exists" : " does not exist")
    );
    if ((await IOUtils.exists(path)) != shouldExist) {
      throw new Error(
        "File " + path + " should " + (shouldExist ? "exist" : "not exist")
      );
    }
  })();
}

function promise_check_contents(path, expect) {
  return (async function () {
    info("Checking whether " + path + " has the right contents");
    let actual = await IOUtils.readJSON(path, {
      decompress: true,
    });
    Assert.deepEqual(
      actual,
      expect,
      `File ${path} contains the expected data.`
    );
  })();
}

// Check whether the migration from .js to .jslz4 is correct.
add_task(async function test_migration() {
  let source = do_get_file("data/sessionstore_valid.js");
  source.copyTo(profd, "sessionstore.js");

  // Read the content of the session store file.
  let parsed = await IOUtils.readJSON(Paths.clean.replace("jsonlz4", "js"));

  // Read the session file with .js extension.
  let result = await SessionFile.read();

  // Check whether the result is what we wanted.
  equal(result.origin, "clean");
  equal(result.useOldExtension, true);
  Assert.deepEqual(
    result.parsed,
    parsed,
    "result.parsed contains expected data"
  );

  // Initiate a write to ensure we write the compressed version.
  await SessionFile.write(parsed);
  await promise_check_exist(Paths.backups, true);
  await promise_check_exist(Paths.clean, false);
  await promise_check_exist(Paths.cleanBackup, true);
  await promise_check_exist(Paths.recovery, true);
  await promise_check_exist(Paths.recoveryBackup, false);
  await promise_check_exist(Paths.nextUpgradeBackup, true);
  // The deprecated $Path.clean should exist.
  await promise_check_exist(Paths.clean.replace("jsonlz4", "js"), true);

  await promise_check_contents(Paths.recovery, parsed);
});

add_task(async function test_startup_with_compressed_clean() {
  let state = { windows: [] };

  // Mare sure we have an empty profile dir.
  await SessionFile.wipe();

  // Populate session files to profile dir.
  await IOUtils.writeJSON(Paths.clean, state, {
    compress: true,
  });
  await IOUtils.makeDirectory(Paths.backups);
  await IOUtils.writeJSON(Paths.cleanBackup, state, {
    compress: true,
  });

  // Initiate a read.
  let result = await SessionFile.read();

  // Make sure we read correct session file and its content.
  equal(result.origin, "clean");
  equal(result.useOldExtension, false);
  Assert.deepEqual(
    state,
    result.parsed,
    "result.parsed contains expected data"
  );
});

add_task(async function test_empty_profile_dir() {
  // Make sure that we have empty profile dir.
  await SessionFile.wipe();
  await promise_check_exist(Paths.backups, false);
  await promise_check_exist(Paths.clean, false);
  await promise_check_exist(Paths.cleanBackup, false);
  await promise_check_exist(Paths.recovery, false);
  await promise_check_exist(Paths.recoveryBackup, false);
  await promise_check_exist(Paths.nextUpgradeBackup, false);
  await promise_check_exist(Paths.backups.replace("jsonlz4", "js"), false);
  await promise_check_exist(Paths.clean.replace("jsonlz4", "js"), false);
  await promise_check_exist(Paths.cleanBackup.replace("lz4", ""), false);
  await promise_check_exist(Paths.recovery.replace("jsonlz4", "js"), false);
  await promise_check_exist(
    Paths.recoveryBackup.replace("jsonlz4", "js"),
    false
  );
  await promise_check_exist(
    Paths.nextUpgradeBackup.replace("jsonlz4", "js"),
    false
  );

  // Initiate a read and make sure that we are in empty state.
  let result = await SessionFile.read();
  equal(result.origin, "empty");
  equal(result.noFilesFound, true);

  // Create a state to store.
  let state = { windows: [] };
  await SessionWriter.write(state, { isFinalWrite: true });

  // Check session files are created, but not deprecated ones.
  await promise_check_exist(Paths.clean, true);
  await promise_check_exist(Paths.clean.replace("jsonlz4", "js"), false);

  // Check session file' content is correct.
  await promise_check_contents(Paths.clean, state);
});