summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_incognito.js
blob: b4b00e7db43d2fb021ccad9456430b023c6341c3 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "42"
);
AddonTestUtils.usePrivilegedSignatures = id => id.startsWith("privileged");

add_task(async function setup() {
  await AddonTestUtils.promiseStartupManager();
});

async function runIncognitoTest(extensionData, privateBrowsingAllowed) {
  let wrapper = ExtensionTestUtils.loadExtension(extensionData);
  await wrapper.startup();
  let { extension } = wrapper;

  equal(
    extension.permissions.has("internal:privateBrowsingAllowed"),
    privateBrowsingAllowed,
    "privateBrowsingAllowed in serialized extension"
  );
  equal(
    extension.privateBrowsingAllowed,
    privateBrowsingAllowed,
    "privateBrowsingAllowed in extension"
  );
  equal(
    extension.policy.privateBrowsingAllowed,
    privateBrowsingAllowed,
    "privateBrowsingAllowed on policy"
  );

  await wrapper.unload();
}

add_task(async function test_extension_incognito_spanning() {
  await runIncognitoTest({}, false);
});

// Test that when we are restricted, we can override the restriction for tests.
add_task(async function test_extension_incognito_override_spanning() {
  let extensionData = {
    incognitoOverride: "spanning",
  };
  await runIncognitoTest(extensionData, true);
});

// This tests that a privileged extension will always have private browsing.
add_task(async function test_extension_incognito_privileged() {
  let extensionData = {
    isPrivileged: true,
  };
  await runIncognitoTest(extensionData, true);
});

add_task(async function test_extension_privileged_not_allowed() {
  let addonId = "privileged_not_allowed@mochi.test";
  let extensionData = {
    manifest: {
      version: "1.0",
      browser_specific_settings: { gecko: { id: addonId } },
      incognito: "not_allowed",
    },
    useAddonManager: "permanent",
    isPrivileged: true,
  };
  let wrapper = ExtensionTestUtils.loadExtension(extensionData);
  await wrapper.startup();
  let policy = WebExtensionPolicy.getByID(addonId);
  equal(
    policy.extension.isPrivileged,
    true,
    "The test extension is privileged"
  );
  equal(
    policy.privateBrowsingAllowed,
    false,
    "privateBrowsingAllowed is false"
  );

  await wrapper.unload();
});

// Test that we remove pb permission if an extension is updated to not_allowed.
add_task(async function test_extension_upgrade_not_allowed() {
  let addonId = "upgrade@mochi.test";
  let extensionData = {
    manifest: {
      version: "1.0",
      browser_specific_settings: { gecko: { id: addonId } },
      incognito: "spanning",
    },
    useAddonManager: "permanent",
    incognitoOverride: "spanning",
  };
  let wrapper = ExtensionTestUtils.loadExtension(extensionData);
  await wrapper.startup();

  let policy = WebExtensionPolicy.getByID(addonId);

  equal(
    policy.privateBrowsingAllowed,
    true,
    "privateBrowsingAllowed in extension"
  );

  extensionData.manifest.version = "2.0";
  extensionData.manifest.incognito = "not_allowed";
  await wrapper.upgrade(extensionData);

  equal(wrapper.version, "2.0", "Expected extension version");
  policy = WebExtensionPolicy.getByID(addonId);
  equal(
    policy.privateBrowsingAllowed,
    false,
    "privateBrowsingAllowed is false"
  );

  await wrapper.unload();
});