summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/tests/xpcshell/test_AddonsBackupResource.js
blob: d1c47ecdb003bd8162e90d49b6023d16379876ad (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Any copyright is dedicated to the Public Domain.
https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

/**
 * Tests that we can measure the size of all the addons & extensions data.
 */
add_task(async function test_measure() {
  Services.fog.testResetFOG();
  Services.telemetry.clearScalars();

  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_JSON = 250;
  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORE = 500;
  const EXPECTED_KILOBYTES_FOR_STORAGE_SYNC = 50;
  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_A = 600;
  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_B = 400;
  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_C = 150;
  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_DIRECTORY = 1000;
  const EXPECTED_KILOBYTES_FOR_EXTENSION_DATA = 100;
  const EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORAGE = 200;

  let tempDir = PathUtils.tempDir;

  // Create extensions json files (all the same size).
  const extensionsFilePath = PathUtils.join(tempDir, "extensions.json");
  await createKilobyteSizedFile(
    extensionsFilePath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_JSON
  );
  const extensionSettingsFilePath = PathUtils.join(
    tempDir,
    "extension-settings.json"
  );
  await createKilobyteSizedFile(
    extensionSettingsFilePath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_JSON
  );
  const extensionsPrefsFilePath = PathUtils.join(
    tempDir,
    "extension-preferences.json"
  );
  await createKilobyteSizedFile(
    extensionsPrefsFilePath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_JSON
  );
  const addonStartupFilePath = PathUtils.join(tempDir, "addonStartup.json.lz4");
  await createKilobyteSizedFile(
    addonStartupFilePath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_JSON
  );

  // Create the extension store permissions data file.
  let extensionStorePermissionsDataSize = PathUtils.join(
    tempDir,
    "extension-store-permissions",
    "data.safe.bin"
  );
  await createKilobyteSizedFile(
    extensionStorePermissionsDataSize,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORE
  );

  // Create the storage sync database file.
  let storageSyncPath = PathUtils.join(tempDir, "storage-sync-v2.sqlite");
  await createKilobyteSizedFile(
    storageSyncPath,
    EXPECTED_KILOBYTES_FOR_STORAGE_SYNC
  );

  // Create the extensions directory with XPI files.
  let extensionsXPIAPath = PathUtils.join(
    tempDir,
    "extensions",
    "extension-b.xpi"
  );
  let extensionsXPIBPath = PathUtils.join(
    tempDir,
    "extensions",
    "extension-a.xpi"
  );
  await createKilobyteSizedFile(
    extensionsXPIAPath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_A
  );
  await createKilobyteSizedFile(
    extensionsXPIBPath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_B
  );
  // Should be ignored.
  let extensionsXPIStagedPath = PathUtils.join(
    tempDir,
    "extensions",
    "staged",
    "staged-test-extension.xpi"
  );
  let extensionsXPITrashPath = PathUtils.join(
    tempDir,
    "extensions",
    "trash",
    "trashed-test-extension.xpi"
  );
  let extensionsXPIUnpackedPath = PathUtils.join(
    tempDir,
    "extensions",
    "unpacked-extension.xpi",
    "manifest.json"
  );
  await createKilobyteSizedFile(
    extensionsXPIStagedPath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_C
  );
  await createKilobyteSizedFile(
    extensionsXPITrashPath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_C
  );
  await createKilobyteSizedFile(
    extensionsXPIUnpackedPath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_XPI_C
  );

  // Create the browser extension data directory.
  let browserExtensionDataPath = PathUtils.join(
    tempDir,
    "browser-extension-data",
    "test-file"
  );
  await createKilobyteSizedFile(
    browserExtensionDataPath,
    EXPECTED_KILOBYTES_FOR_EXTENSION_DATA
  );

  // Create the extensions storage directory.
  let extensionsStoragePath = PathUtils.join(
    tempDir,
    "storage",
    "default",
    "moz-extension+++test-extension-id",
    "idb",
    "data.sqlite"
  );
  // Other storage files that should not be counted.
  let otherStoragePath = PathUtils.join(
    tempDir,
    "storage",
    "default",
    "https+++accounts.firefox.com",
    "ls",
    "data.sqlite"
  );

  await createKilobyteSizedFile(
    extensionsStoragePath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORAGE
  );
  await createKilobyteSizedFile(
    otherStoragePath,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORAGE
  );

  // Measure all the extensions data.
  let extensionsBackupResource = new AddonsBackupResource();
  await extensionsBackupResource.measure(tempDir);

  let extensionsJsonSizeMeasurement =
    Glean.browserBackup.extensionsJsonSize.testGetValue();
  Assert.equal(
    extensionsJsonSizeMeasurement,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_JSON * 4, // There are 4 equally sized files.
    "Should have collected the correct measurement of the total size of all extensions JSON files"
  );

  let extensionStorePermissionsDataSizeMeasurement =
    Glean.browserBackup.extensionStorePermissionsDataSize.testGetValue();
  Assert.equal(
    extensionStorePermissionsDataSizeMeasurement,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORE,
    "Should have collected the correct measurement of the size of the extension store permissions data"
  );

  let storageSyncSizeMeasurement =
    Glean.browserBackup.storageSyncSize.testGetValue();
  Assert.equal(
    storageSyncSizeMeasurement,
    EXPECTED_KILOBYTES_FOR_STORAGE_SYNC,
    "Should have collected the correct measurement of the size of the storage sync database"
  );

  let extensionsXPIDirectorySizeMeasurement =
    Glean.browserBackup.extensionsXpiDirectorySize.testGetValue();
  Assert.equal(
    extensionsXPIDirectorySizeMeasurement,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_DIRECTORY,
    "Should have collected the correct measurement of the size 2 equally sized XPI files in the extensions directory"
  );

  let browserExtensionDataSizeMeasurement =
    Glean.browserBackup.browserExtensionDataSize.testGetValue();
  Assert.equal(
    browserExtensionDataSizeMeasurement,
    EXPECTED_KILOBYTES_FOR_EXTENSION_DATA,
    "Should have collected the correct measurement of the size of the browser extension data directory"
  );

  let extensionsStorageSizeMeasurement =
    Glean.browserBackup.extensionsStorageSize.testGetValue();
  Assert.equal(
    extensionsStorageSizeMeasurement,
    EXPECTED_KILOBYTES_FOR_EXTENSIONS_STORAGE,
    "Should have collected the correct measurement of all the extensions storage"
  );

  // Compare glean vs telemetry measurements
  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.extensions_json_size",
    extensionsJsonSizeMeasurement,
    "Glean and telemetry measurements for extensions JSON should be equal"
  );
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.extension_store_permissions_data_size",
    extensionStorePermissionsDataSizeMeasurement,
    "Glean and telemetry measurements for extension store permissions data should be equal"
  );
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.storage_sync_size",
    storageSyncSizeMeasurement,
    "Glean and telemetry measurements for storage sync database should be equal"
  );
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.extensions_xpi_directory_size",
    extensionsXPIDirectorySizeMeasurement,
    "Glean and telemetry measurements for extensions directory should be equal"
  );
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.browser_extension_data_size",
    browserExtensionDataSizeMeasurement,
    "Glean and telemetry measurements for browser extension data should be equal"
  );
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.extensions_storage_size",
    extensionsStorageSizeMeasurement,
    "Glean and telemetry measurements for extensions storage should be equal"
  );

  await maybeRemovePath(tempDir);
});

/**
 * Tests that we can handle the extension store permissions data
 * and moz-extension IndexedDB databases not existing.
 */
add_task(async function test_measure_missing_data() {
  Services.fog.testResetFOG();

  let tempDir = PathUtils.tempDir;

  let extensionsBackupResource = new AddonsBackupResource();
  await extensionsBackupResource.measure(tempDir);

  let extensionStorePermissionsDataSizeMeasurement =
    Glean.browserBackup.extensionStorePermissionsDataSize.testGetValue();
  Assert.equal(
    extensionStorePermissionsDataSizeMeasurement,
    null,
    "Should NOT have collected a measurement for the missing permissions data"
  );

  let extensionsStorageSizeMeasurement =
    Glean.browserBackup.extensionsStorageSize.testGetValue();
  Assert.equal(
    extensionsStorageSizeMeasurement,
    null,
    "Should NOT have collected a measurement for the missing storage data"
  );
});

/**
 * Test that the backup method correctly copies items from the profile directory
 * into the staging directory.
 */
add_task(async function test_backup() {
  let sandbox = sinon.createSandbox();

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

  const simpleCopyFiles = [
    { path: "extensions.json" },
    { path: "extension-settings.json" },
    { path: "extension-preferences.json" },
    { path: "addonStartup.json.lz4" },
    {
      path: [
        "browser-extension-data",
        "{11aa1234-f111-1234-abcd-a9b8c7654d32}",
      ],
    },
    { path: ["extension-store-permissions", "data.safe.bin"] },
    { path: ["extensions", "{11aa1234-f111-1234-abcd-a9b8c7654d32}.xpi"] },
  ];
  await createTestFiles(sourcePath, simpleCopyFiles);

  const junkFiles = [{ path: ["extensions", "junk"] }];
  await createTestFiles(sourcePath, junkFiles);

  // Create a fake storage-sync-v2 database file. 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: "storage-sync-v2.sqlite" }]);

  let fakeConnection = {
    backup: sandbox.stub().resolves(true),
    close: sandbox.stub().resolves(true),
  };
  sandbox.stub(Sqlite, "openConnection").returns(fakeConnection);

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

  await assertFilesExist(stagingPath, simpleCopyFiles);

  let junkFile = PathUtils.join(stagingPath, "extensions", "junk");
  Assert.equal(
    await IOUtils.exists(junkFile),
    false,
    `${junkFile} should not exist in the staging folder`
  );

  // Make sure storage-sync-v2 database is backed up.
  Assert.ok(
    fakeConnection.backup.calledOnce,
    "Called backup the expected number of times for all connections"
  );
  Assert.ok(
    fakeConnection.backup.calledWith(
      PathUtils.join(stagingPath, "storage-sync-v2.sqlite")
    ),
    "Called backup on the storage-sync-v2 Sqlite connection"
  );

  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 addonsBackupResource = new AddonsBackupResource();
  let recoveryPath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "addonsBackupResource-recovery-test"
  );
  let destProfilePath = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "addonsBackupResource-test-profile"
  );

  const files = [
    { path: "extensions.json" },
    { path: "extension-settings.json" },
    { path: "extension-preferences.json" },
    { path: "addonStartup.json.lz4" },
    { path: "storage-sync-v2.sqlite" },
    { path: ["browser-extension-data", "addon@darkreader.org.xpi", "data"] },
    { path: ["extensions", "addon@darkreader.org.xpi"] },
    { path: ["extension-store-permissions", "data.safe.bin"] },
  ];
  await createTestFiles(recoveryPath, files);

  // The backup method is expected to have returned a null ManifestEntry
  let postRecoveryEntry = await addonsBackupResource.recover(
    null /* manifestEntry */,
    recoveryPath,
    destProfilePath
  );
  Assert.equal(
    postRecoveryEntry,
    null,
    "AddonsBackupResource.recover should return null as its post " +
      "recovery entry"
  );

  await assertFilesExist(destProfilePath, files);

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