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

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

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.usePrivilegedSignatures = id => id.startsWith("privileged");

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "42", "42");

add_setup(async () => {
  await AddonTestUtils.promiseStartupManager();
});

function assertQuarantineIgnoredByUserPrefsRemoved() {
  const { PREF_ADDONS_BRANCH_NAME } = QuarantinedDomains;
  const prefBranch = Services.prefs.getBranch(PREF_ADDONS_BRANCH_NAME);
  for (const prefSuffix of prefBranch.getChildList("")) {
    Assert.equal(
      prefBranch.getPrefType(prefSuffix),
      prefBranch.PREF_INVALID,
      `${PREF_ADDONS_BRANCH_NAME}${prefSuffix} pref should have been removed`
    );
  }
}

async function testQuarantineDomainsAddonWrapperProperties() {
  // Make sure no extension is initially user exempted.
  const prefBranch = Services.prefs.getBranch(
    QuarantinedDomains.PREF_ADDONS_BRANCH_NAME
  );
  for (const leafName of prefBranch.getChildList("")) {
    Services.prefs.clearUserPref(
      QuarantinedDomains.PREF_ADDONS_BRANCH_NAME + leafName
    );
  }

  const REGULAR_EXT_ID = "regular@ext.id";
  const PRIVILEGE_EXT_ID = "privileged@ext.id";
  const RECOMMENDED_EXT_ID = "recommended@ext.id";

  const regularExt = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",
    manifest: {
      browser_specific_settings: { gecko: { id: REGULAR_EXT_ID } },
    },
  });

  const privilegedExt = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",
    manifest: {
      browser_specific_settings: { gecko: { id: PRIVILEGE_EXT_ID } },
    },
  });

  const testStartTime = Date.now();
  // Keep the recommendations validity range used here in sync with
  // the one used in test_recommendations.js.
  const not_before = new Date(testStartTime - 3600000).toISOString();
  const not_after = new Date(testStartTime + 3600000).toISOString();
  const RECOMMENDATION_FILE_NAME = "mozilla-recommendation.json";
  const recommendedExt = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",
    manifest: {
      browser_specific_settings: { gecko: { id: RECOMMENDED_EXT_ID } },
    },
    files: {
      [RECOMMENDATION_FILE_NAME]: {
        addon_id: RECOMMENDED_EXT_ID,
        states: ["fake", "states"],
        validity: { not_before, not_after },
      },
    },
  });

  await regularExt.startup();
  await privilegedExt.startup();
  await recommendedExt.startup();

  function assertAddonWrapperProps(addon, expectedProps) {
    const expectedPropNames = Object.keys(expectedProps);
    if (!expectedPropNames.length) {
      throw new Error("expectedProps shouldn't be empty");
    }
    for (const propName of expectedPropNames) {
      Assert.deepEqual(
        addon[propName],
        expectedProps[propName],
        `Got the expected value on ${propName} property from ${addon.id}`
      );
    }
  }

  const EXPECTED_PROPS_REGULAR_EXT = {
    id: regularExt.id,
    isPrivileged: false,
    recommendationStates: [],
    quarantineIgnoredByApp: false,
    quarantineIgnoredByUser: false,
    canChangeQuarantineIgnored: true,
  };

  const EXPECTED_PROPS_PRIVILEGED_EXT = {
    id: privilegedExt.id,
    isPrivileged: true,
    recommendationStates: [],
    // Expected to be true due to privileged signature.
    quarantineIgnoredByApp: true,
    quarantineIgnoredByUser: false,
    // Expected to be false for app allowed.
    canChangeQuarantineIgnored: false,
  };

  const EXPECTED_PROPS_RECOMMENDED_EXT = {
    id: recommendedExt.id,
    isPrivileged: false,
    recommendationStates: ["fake", "states"],
    // Expected to be true due to recommendationStates.
    quarantineIgnoredByApp: true,
    quarantineIgnoredByUser: false,
    // Expected to be false for app allowed.
    canChangeQuarantineIgnored: false,
  };

  assertAddonWrapperProps(regularExt.addon, EXPECTED_PROPS_REGULAR_EXT);

  assertAddonWrapperProps(privilegedExt.addon, EXPECTED_PROPS_PRIVILEGED_EXT);

  assertAddonWrapperProps(recommendedExt.addon, EXPECTED_PROPS_RECOMMENDED_EXT);

  info("Verify quarantineIgnoredByUser property changed");
  let promisePropChanged =
    AddonTestUtils.promiseAddonEvent("onPropertyChanged");
  regularExt.addon.quarantineIgnoredByUser = true;
  assertAddonWrapperProps(regularExt.addon, {
    ...EXPECTED_PROPS_REGULAR_EXT,
    quarantineIgnoredByUser: true,
  });
  info("Wait for onPropertyChanged listener to be called");
  let [addon, props] = await promisePropChanged;
  Assert.deepEqual(
    {
      addonId: addon.id,
      props,
    },
    {
      addonId: regularExt.id,
      props: ["quarantineIgnoredByUser"],
    },
    "Got the expected params from onPropertyChanged listener call"
  );
  Services.prefs.clearUserPref(
    QuarantinedDomains.getUserAllowedAddonIdPrefName(regularExt.id)
  );

  info("Verify canChangeQuarantineIgnored on quarantineDomainsEnabled false");
  Services.prefs.setBoolPref("extensions.quarantinedDomains.enabled", false);
  assertAddonWrapperProps(regularExt.addon, {
    ...EXPECTED_PROPS_REGULAR_EXT,
    canChangeQuarantineIgnored: false,
  });
  Services.prefs.clearUserPref("extensions.quarantinedDomains.enabled");
  assertAddonWrapperProps(regularExt.addon, EXPECTED_PROPS_REGULAR_EXT);

  info("Verify canChangeQuarantineIgnored on uiDisabled true");
  Services.prefs.setBoolPref("extensions.quarantinedDomains.uiDisabled", true);
  assertAddonWrapperProps(regularExt.addon, {
    ...EXPECTED_PROPS_REGULAR_EXT,
    canChangeQuarantineIgnored: false,
  });
  Services.prefs.clearUserPref("extensions.quarantinedDomains.uiDisabled");
  assertAddonWrapperProps(regularExt.addon, EXPECTED_PROPS_REGULAR_EXT);

  info(
    "Verify that the per-addon quarantineIgnoredByUser pref is removed on addon uninstall"
  );

  promisePropChanged = AddonTestUtils.promiseAddonEvent("onPropertyChanged");
  regularExt.addon.quarantineIgnoredByUser = true;
  await promisePropChanged;
  Assert.equal(
    Services.prefs.getBoolPref(
      QuarantinedDomains.getUserAllowedAddonIdPrefName(regularExt.id)
    ),
    true,
    "Expect the per-addon quarantineIgnoredByUser to be set"
  );

  await recommendedExt.unload();
  await privilegedExt.unload();
  await regularExt.unload();

  assertQuarantineIgnoredByUserPrefsRemoved();
}

add_task(
  {
    pref_set: [
      ["extensions.quarantinedDomains.enabled", true],
      ["extensions.quarantinedDomains.uiDisabled", false],
    ],
  },
  testQuarantineDomainsAddonWrapperProperties
);