summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/tests/xpcshell/test_MiscDataBackupResource.js
blob: e57dd50cd38dcf2e492b8fcece80bfa3eb0adc63 (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
/* 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"
);

/**
 * 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 = 241;
  const tempDir = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "MiscDataBackupResource-measurement-test"
  );

  const mockFiles = [
    { path: "times.json", sizeInKB: 5 },
    { 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: "times.json" },
    { path: "enumerate_devices.txt" },
    { path: "SiteSecurityServiceState.bin" },
  ];
  await createTestFiles(sourcePath, simpleCopyFiles);

  // 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);

  await miscDataBackupResource.backup(stagingPath, sourcePath);

  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 ability to copy the
  // chrome-privileged IndexedDB databases under storage/permanent/chrome, so
  // we'll just skip testing that for now.

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

  sandbox.restore();
});