summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/tests/xpcshell/test_MiscDataBackupResource.js
blob: ab63b65332d6287f24d3ab481749db3f73da168d (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Any copyright is dedicated to the Public Domain.
https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

const { ActivityStreamStorage } = ChromeUtils.importESModule(
  "resource://activity-stream/lib/ActivityStreamStorage.sys.mjs"
);

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

/**
 * Tests that we can measure miscellaneous files in the profile directory.
 */
add_task(async function test_measure() {
  Services.fog.testResetFOG();

  const EXPECTED_MISC_KILOBYTES_SIZE = 231;
  const tempDir = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-measurement-test"
  );

  const mockFiles = [
    { path: "enumerate_devices.txt", sizeInKB: 1 },
    { path: "protections.sqlite", sizeInKB: 100 },
    { path: "SiteSecurityServiceState.bin", sizeInKB: 10 },
    { path: ["storage", "permanent", "chrome", "123ABC.sqlite"], sizeInKB: 40 },
    { path: ["storage", "permanent", "chrome", "456DEF.sqlite"], sizeInKB: 40 },
    {
      path: ["storage", "permanent", "chrome", "mockIDBDir", "890HIJ.sqlite"],
      sizeInKB: 40,
    },
  ];

  await createTestFiles(tempDir, mockFiles);

  let miscDataBackupResource = new MiscDataBackupResource();
  await miscDataBackupResource.measure(tempDir);

  let measurement = Glean.browserBackup.miscDataSize.testGetValue();
  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);

  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.misc_data_size",
    measurement,
    "Glean and telemetry measurements for misc data should be equal"
  );
  Assert.equal(
    measurement,
    EXPECTED_MISC_KILOBYTES_SIZE,
    "Should have collected the correct glean measurement for misc files"
  );

  await maybeRemovePath(tempDir);
});

add_task(async function test_backup() {
  let sandbox = sinon.createSandbox();

  let miscDataBackupResource = new MiscDataBackupResource();
  let sourcePath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-source-test"
  );
  let stagingPath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-staging-test"
  );

  const simpleCopyFiles = [
    { path: "enumerate_devices.txt" },
    { path: "SiteSecurityServiceState.bin" },
  ];
  await createTestFiles(sourcePath, simpleCopyFiles);

  // Create our fake database files. We don't expect this to be copied to the
  // staging directory in this test due to our stubbing of the backup method, so
  // we don't include it in `simpleCopyFiles`.
  await createTestFiles(sourcePath, [{ path: "protections.sqlite" }]);

  // We have no need to test that Sqlite.sys.mjs's backup method is working -
  // this is something that is tested in Sqlite's own tests. We can just make
  // sure that it's being called using sinon. Unfortunately, we cannot do the
  // same thing with IOUtils.copy, as its methods are not stubbable.
  let fakeConnection = {
    backup: sandbox.stub().resolves(true),
    close: sandbox.stub().resolves(true),
  };
  sandbox.stub(Sqlite, "openConnection").returns(fakeConnection);

  let snippetsTableStub = {
    getAllKeys: sandbox.stub().resolves(["key1", "key2"]),
    get: sandbox.stub().callsFake(key => {
      return { key: `value for ${key}` };
    }),
  };

  sandbox
    .stub(ActivityStreamStorage.prototype, "getDbTable")
    .withArgs("snippets")
    .resolves(snippetsTableStub);

  let manifestEntry = await miscDataBackupResource.backup(
    stagingPath,
    sourcePath
  );
  Assert.equal(
    manifestEntry,
    null,
    "MiscDataBackupResource.backup should return null as its ManifestEntry"
  );

  await assertFilesExist(stagingPath, simpleCopyFiles);

  // Next, we'll make sure that the Sqlite connection had `backup` called on it
  // with the right arguments.
  Assert.ok(
    fakeConnection.backup.calledOnce,
    "Called backup the expected number of times for all connections"
  );
  Assert.ok(
    fakeConnection.backup.firstCall.calledWith(
      PathUtils.join(stagingPath, "protections.sqlite")
    ),
    "Called backup on the protections.sqlite Sqlite connection"
  );

  // Bug 1890585 - we don't currently have the generalized ability to copy the
  // chrome-privileged IndexedDB databases under storage/permanent/chrome, but
  // we do support copying individual IndexedDB databases by manually exporting
  // and re-importing their contents.
  let snippetsBackupPath = PathUtils.join(
    stagingPath,
    "activity-stream-snippets.json"
  );
  Assert.ok(
    await IOUtils.exists(snippetsBackupPath),
    "The activity-stream-snippets.json file should exist"
  );
  let snippetsBackupContents = await IOUtils.readJSON(snippetsBackupPath);
  Assert.deepEqual(
    snippetsBackupContents,
    {
      key1: { key: "value for key1" },
      key2: { key: "value for key2" },
    },
    "The contents of the activity-stream-snippets.json file should be as expected"
  );

  await maybeRemovePath(stagingPath);
  await maybeRemovePath(sourcePath);

  sandbox.restore();
});

/**
 * Test that the recover method correctly copies items from the recovery
 * directory into the destination profile directory.
 */
add_task(async function test_recover() {
  let miscBackupResource = new MiscDataBackupResource();
  let recoveryPath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-recovery-test"
  );
  let destProfilePath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-test-profile"
  );

  // Write a dummy times.json into the xpcshell test profile directory. We
  // expect it to be copied into the destination profile.
  let originalProfileAge = await ProfileAge(PathUtils.profileDir);
  await originalProfileAge.computeAndPersistCreated();
  Assert.ok(
    await IOUtils.exists(PathUtils.join(PathUtils.profileDir, "times.json"))
  );

  const simpleCopyFiles = [
    { path: "enumerate_devices.txt" },
    { path: "protections.sqlite" },
    { path: "SiteSecurityServiceState.bin" },
  ];
  await createTestFiles(recoveryPath, simpleCopyFiles);

  const SNIPPETS_BACKUP_FILE = "activity-stream-snippets.json";

  // We'll also separately create the activity-stream-snippets.json file, which
  // is not expected to be copied into the profile directory, but is expected
  // to exist in the recovery path.
  await createTestFiles(recoveryPath, [{ path: SNIPPETS_BACKUP_FILE }]);

  // The backup method is expected to have returned a null ManifestEntry
  let postRecoveryEntry = await miscBackupResource.recover(
    null /* manifestEntry */,
    recoveryPath,
    destProfilePath
  );
  Assert.deepEqual(
    postRecoveryEntry,
    {
      snippetsBackupFile: PathUtils.join(recoveryPath, SNIPPETS_BACKUP_FILE),
    },
    "MiscDataBackupResource.recover should return the snippets backup data " +
      "path as its post recovery entry"
  );

  await assertFilesExist(destProfilePath, simpleCopyFiles);

  // The activity-stream-snippets.json path should _not_ have been written to
  // the profile path.
  Assert.ok(
    !(await IOUtils.exists(
      PathUtils.join(destProfilePath, SNIPPETS_BACKUP_FILE)
    )),
    "Snippets backup data should not have gone into the profile directory"
  );

  // The times.json file should have been copied over and a backup recovery
  // time written into it.
  Assert.ok(
    await IOUtils.exists(PathUtils.join(destProfilePath, "times.json"))
  );
  let copiedProfileAge = await ProfileAge(destProfilePath);
  Assert.equal(
    await originalProfileAge.created,
    await copiedProfileAge.created,
    "Created timestamp should match."
  );
  Assert.equal(
    await originalProfileAge.firstUse,
    await copiedProfileAge.firstUse,
    "First use timestamp should match."
  );
  Assert.ok(
    await copiedProfileAge.recoveredFromBackup,
    "Backup recovery timestamp should have been set."
  );

  await maybeRemovePath(recoveryPath);
  await maybeRemovePath(destProfilePath);
});

/**
 * Test that the postRecovery method correctly writes the snippets backup data
 * into the snippets IndexedDB table.
 */
add_task(async function test_postRecovery() {
  let sandbox = sinon.createSandbox();

  let fakeProfilePath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-test-profile"
  );
  let fakeSnippetsData = {
    key1: "value1",
    key2: "value2",
  };
  const SNIPPEST_BACKUP_FILE = PathUtils.join(
    fakeProfilePath,
    "activity-stream-snippets.json"
  );

  await IOUtils.writeJSON(SNIPPEST_BACKUP_FILE, fakeSnippetsData);

  let snippetsTableStub = {
    set: sandbox.stub(),
  };

  sandbox
    .stub(ActivityStreamStorage.prototype, "getDbTable")
    .withArgs("snippets")
    .resolves(snippetsTableStub);

  let miscBackupResource = new MiscDataBackupResource();
  await miscBackupResource.postRecovery({
    snippetsBackupFile: SNIPPEST_BACKUP_FILE,
  });

  Assert.ok(
    snippetsTableStub.set.calledTwice,
    "The snippets table's set method was called twice"
  );
  Assert.ok(
    snippetsTableStub.set.firstCall.calledWith("key1", "value1"),
    "The snippets table's set method was called with the first key-value pair"
  );
  Assert.ok(
    snippetsTableStub.set.secondCall.calledWith("key2", "value2"),
    "The snippets table's set method was called with the second key-value pair"
  );

  sandbox.restore();
});