summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_QuarantinedDomains_telemetry.js
blob: c18785b3d6cb09bfef165453bb6dd2d837b00934 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

AddonTestUtils.init(this);
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "43"
);

ChromeUtils.defineESModuleGetters(this, {
  computeSha1HashAsString: "resource://gre/modules/addons/crypto-utils.sys.mjs",
  QuarantinedDomains: "resource://gre/modules/ExtensionPermissions.sys.mjs",
});

add_setup(() => {
  setupTelemetryForTests();
});

add_task(async function test_QuarantinedDomainsList_telemetry() {
  const cleanupPrefs = () => {
    Services.prefs.clearUserPref(QuarantinedDomains.PREF_DOMAINSLIST_NAME);
  };
  registerCleanupFunction(cleanupPrefs);

  const assertDomainsListTelemetry = ({ prefValue, expected }) => {
    resetTelemetryData();
    Services.prefs.setStringPref(
      QuarantinedDomains.PREF_DOMAINSLIST_NAME,
      prefValue
    );
    Assert.deepEqual(
      {
        listsize: QuarantinedDomains.currentDomainsList.set.size,
        listhash: QuarantinedDomains.currentDomainsList.hash,
      },
      expected,
      "Got the expected domains list data computed for the probes"
    );
    Assert.deepEqual(
      {
        listsize: Glean.extensionsQuarantinedDomains.listsize.testGetValue(),
        listhash: Glean.extensionsQuarantinedDomains.listhash.testGetValue(),
      },
      expected,
      "Got the expected computed domains list probes recorded by the Glean metrics"
    );
    const scalars = Services.telemetry.getSnapshotForScalars().parent;
    Assert.deepEqual(
      {
        listsize: scalars?.["extensions.quarantinedDomains.listsize"],
        listhash: scalars?.["extensions.quarantinedDomains.listhash"],
      },
      expected,
      "Got the expected metrics mirrored into the unified telemetry scalars"
    );
  };

  let prefValue;

  info("Verify Glean 'Quarantined Domains list' probes on empty domain list");
  prefValue = "";
  assertDomainsListTelemetry({
    prefValue,
    expected: {
      listsize: 0,
      listhash: computeSha1HashAsString(prefValue),
    },
  });

  info(
    "Verify Glean 'Quarantined Domains list' probes on non-empty domain list"
  );
  prefValue = "example.com,example.org";
  assertDomainsListTelemetry({
    prefValue,
    expected: {
      listsize: 2,
      listhash: computeSha1HashAsString(prefValue),
    },
  });

  info(
    "Verify Glean 'Quarantined Domains list' probes on non-empty domain list with duplicated domains"
  );
  prefValue = "example.com,example.org, example.org, example.com ";
  assertDomainsListTelemetry({
    prefValue,
    expected: {
      listsize: 2,
      listhash: computeSha1HashAsString(prefValue),
    },
  });

  cleanupPrefs();
});