summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/tests/xpcshell/test_measurements.js
blob: 0dece6b370798be8e61d0368ce94d7d4669800fe (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

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

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

add_setup(() => {
  // FOG needs to be initialized in order for data to flow.
  Services.fog.initializeFOG();
  Services.telemetry.clearScalars();
});

/**
 * Tests that calling `BackupService.takeMeasurements` will call the measure
 * method of all registered BackupResource classes.
 */
add_task(async function test_takeMeasurements() {
  let sandbox = sinon.createSandbox();
  sandbox.stub(FakeBackupResource1.prototype, "measure").resolves();
  sandbox
    .stub(FakeBackupResource2.prototype, "measure")
    .rejects(new Error("Some failure to measure"));

  let bs = new BackupService({ FakeBackupResource1, FakeBackupResource2 });
  await bs.takeMeasurements();

  for (let backupResourceClass of [FakeBackupResource1, FakeBackupResource2]) {
    Assert.ok(
      backupResourceClass.prototype.measure.calledOnce,
      "Measure was called"
    );
    Assert.ok(
      backupResourceClass.prototype.measure.calledWith(PathUtils.profileDir),
      "Measure was called with the profile directory argument"
    );
  }

  sandbox.restore();
});

/**
 * Tests that we can measure the disk space available in the profile directory.
 */
add_task(async function test_profDDiskSpace() {
  let bs = new BackupService();
  await bs.takeMeasurements();
  let measurement = Glean.browserBackup.profDDiskSpace.testGetValue();
  TelemetryTestUtils.assertScalar(
    TelemetryTestUtils.getProcessScalars("parent", false, true),
    "browser.backup.prof_d_disk_space",
    measurement
  );

  Assert.greater(
    measurement,
    0,
    "Should have collected a measurement for the profile directory storage " +
      "device"
  );
});

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

  const EXPECTED_CREDENTIALS_KILOBYTES_SIZE = 413;
  const EXPECTED_SECURITY_KILOBYTES_SIZE = 231;

  // Create resource files in temporary directory
  const tempDir = await IOUtils.createUniqueDirectory(
    PathUtils.tempDir,
    "CredentialsAndSecurityBackupResource-measurement-test"
  );

  const mockFiles = [
    // Set up credentials files
    { path: "key4.db", sizeInKB: 300 },
    { path: "logins.json", sizeInKB: 1 },
    { path: "logins-backup.json", sizeInKB: 1 },
    { path: "autofill-profiles.json", sizeInKB: 1 },
    { path: "credentialstate.sqlite", sizeInKB: 100 },
    { path: "signedInUser.json", sizeInKB: 5 },
    // Set up security files
    { path: "cert9.db", sizeInKB: 230 },
    { path: "pkcs11.txt", sizeInKB: 1 },
  ];

  await createTestFiles(tempDir, mockFiles);

  let credentialsAndSecurityBackupResource =
    new CredentialsAndSecurityBackupResource();
  await credentialsAndSecurityBackupResource.measure(tempDir);

  let credentialsMeasurement =
    Glean.browserBackup.credentialsDataSize.testGetValue();
  let securityMeasurement = Glean.browserBackup.securityDataSize.testGetValue();
  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);

  // Credentials measurements
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.credentials_data_size",
    credentialsMeasurement,
    "Glean and telemetry measurements for credentials data should be equal"
  );

  Assert.equal(
    credentialsMeasurement,
    EXPECTED_CREDENTIALS_KILOBYTES_SIZE,
    "Should have collected the correct glean measurement for credentials files"
  );

  // Security measurements
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.security_data_size",
    securityMeasurement,
    "Glean and telemetry measurements for security data should be equal"
  );
  Assert.equal(
    securityMeasurement,
    EXPECTED_SECURITY_KILOBYTES_SIZE,
    "Should have collected the correct glean measurement for security files"
  );

  // Cleanup
  await maybeRemovePath(tempDir);
});

/**
 * Tests that we can measure the Cookies db in a profile directory.
 */
add_task(async function test_cookiesBackupResource() {
  const EXPECTED_COOKIES_DB_SIZE = 1230;

  Services.fog.testResetFOG();

  // Create resource files in temporary directory
  let tempDir = PathUtils.tempDir;
  let tempCookiesDBPath = PathUtils.join(tempDir, "cookies.sqlite");
  await createKilobyteSizedFile(tempCookiesDBPath, EXPECTED_COOKIES_DB_SIZE);

  let cookiesBackupResource = new CookiesBackupResource();
  await cookiesBackupResource.measure(tempDir);

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

  // Compare glean vs telemetry measurements
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.cookies_size",
    cookiesMeasurement,
    "Glean and telemetry measurements for cookies.sqlite should be equal"
  );

  // Compare glean measurements vs actual file sizes
  Assert.equal(
    cookiesMeasurement,
    EXPECTED_COOKIES_DB_SIZE,
    "Should have collected the correct glean measurement for cookies.sqlite"
  );

  await maybeRemovePath(tempCookiesDBPath);
});

/**
 * Tests that we can measure the Form History db in a profile directory.
 */
add_task(async function test_formHistoryBackupResource() {
  const EXPECTED_FORM_HISTORY_DB_SIZE = 500;

  Services.fog.testResetFOG();

  // Create resource files in temporary directory
  let tempDir = PathUtils.tempDir;
  let tempFormHistoryDBPath = PathUtils.join(tempDir, "formhistory.sqlite");
  await createKilobyteSizedFile(
    tempFormHistoryDBPath,
    EXPECTED_FORM_HISTORY_DB_SIZE
  );

  let formHistoryBackupResource = new FormHistoryBackupResource();
  await formHistoryBackupResource.measure(tempDir);

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

  // Compare glean vs telemetry measurements
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.form_history_size",
    formHistoryMeasurement,
    "Glean and telemetry measurements for formhistory.sqlite should be equal"
  );

  // Compare glean measurements vs actual file sizes
  Assert.equal(
    formHistoryMeasurement,
    EXPECTED_FORM_HISTORY_DB_SIZE,
    "Should have collected the correct glean measurement for formhistory.sqlite"
  );

  await IOUtils.remove(tempFormHistoryDBPath);
});

/**
 * Tests that we can measure the Session Store JSON and backups directory.
 */
add_task(async function test_sessionStoreBackupResource() {
  const EXPECTED_KILOBYTES_FOR_BACKUPS_DIR = 1000;
  Services.fog.testResetFOG();

  // Create the sessionstore-backups directory.
  let tempDir = PathUtils.tempDir;
  let sessionStoreBackupsPath = PathUtils.join(
    tempDir,
    "sessionstore-backups",
    "restore.jsonlz4"
  );
  await createKilobyteSizedFile(
    sessionStoreBackupsPath,
    EXPECTED_KILOBYTES_FOR_BACKUPS_DIR
  );

  let sessionStoreBackupResource = new SessionStoreBackupResource();
  await sessionStoreBackupResource.measure(tempDir);

  let sessionStoreBackupsDirectoryMeasurement =
    Glean.browserBackup.sessionStoreBackupsDirectorySize.testGetValue();
  let sessionStoreMeasurement =
    Glean.browserBackup.sessionStoreSize.testGetValue();
  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);

  // Compare glean vs telemetry measurements
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.session_store_backups_directory_size",
    sessionStoreBackupsDirectoryMeasurement,
    "Glean and telemetry measurements for session store backups directory should be equal"
  );
  TelemetryTestUtils.assertScalar(
    scalars,
    "browser.backup.session_store_size",
    sessionStoreMeasurement,
    "Glean and telemetry measurements for session store should be equal"
  );

  // Compare glean measurements vs actual file sizes
  Assert.equal(
    sessionStoreBackupsDirectoryMeasurement,
    EXPECTED_KILOBYTES_FOR_BACKUPS_DIR,
    "Should have collected the correct glean measurement for the sessionstore-backups directory"
  );

  // Session store measurement is from `getCurrentState`, so exact size is unknown.
  Assert.greater(
    sessionStoreMeasurement,
    0,
    "Should have collected a measurement for the session store"
  );

  await IOUtils.remove(sessionStoreBackupsPath);
});

/**
 * Tests that we can measure the size of all the addons & extensions data.
 */
add_task(async function test_AddonsBackupResource() {
  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 not existing.
 */
add_task(
  async function test_AddonsBackupResource_no_extension_store_permissions_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 data"
    );
  }
);

/**
 * Tests that we can handle a profile with no moz-extension IndexedDB databases.
 */
add_task(
  async function test_AddonsBackupResource_no_extension_storage_databases() {
    Services.fog.testResetFOG();

    let tempDir = PathUtils.tempDir;

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

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