summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_unlimitedStorage.js
blob: 77eb0c89f700ccc444c9be2ba6d067a694d65fec (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
"use strict";

const {
  createAppInfo,
  promiseStartupManager,
  promiseRestartManager,
  promiseWebExtensionStartup,
} = AddonTestUtils;

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();

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

const STORAGE_SITE_PERMISSIONS = [
  "WebExtensions-unlimitedStorage",
  "persistent-storage",
];

function checkSitePermissions(principal, expectedPermAction, assertMessage) {
  for (const permName of STORAGE_SITE_PERMISSIONS) {
    const actualPermAction = Services.perms.testPermissionFromPrincipal(
      principal,
      permName
    );

    equal(
      actualPermAction,
      expectedPermAction,
      `The extension "${permName}" SitePermission ${assertMessage} as expected`
    );
  }
}

add_task(async function test_unlimitedStorage_restored_on_app_startup() {
  const id = "test-unlimitedStorage-removed-on-app-shutdown@mozilla";

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["unlimitedStorage"],
      browser_specific_settings: {
        gecko: { id },
      },
    },

    useAddonManager: "permanent",
  });

  await promiseStartupManager();
  await extension.startup();

  const policy = WebExtensionPolicy.getByID(extension.id);
  const principal = policy.extension.principal;

  checkSitePermissions(
    principal,
    Services.perms.ALLOW_ACTION,
    "has been allowed"
  );

  // Remove site permissions as it would happen if Firefox is shutting down
  // with the "clear site permissions" setting.

  Services.perms.removeFromPrincipal(
    principal,
    "WebExtensions-unlimitedStorage"
  );
  Services.perms.removeFromPrincipal(principal, "persistent-storage");

  checkSitePermissions(principal, Services.perms.UNKNOWN_ACTION, "is not set");

  const onceExtensionStarted = promiseWebExtensionStartup(id);
  await promiseRestartManager();
  await onceExtensionStarted;

  // The site permissions should have been granted again.
  checkSitePermissions(
    principal,
    Services.perms.ALLOW_ACTION,
    "has been allowed"
  );

  await extension.unload();
});

add_task(async function test_unlimitedStorage_removed_on_update() {
  const id = "test-unlimitedStorage-removed-on-update@mozilla";

  function background() {
    browser.test.onMessage.addListener(async msg => {
      switch (msg) {
        case "set-storage":
          browser.test.log(`storing data in storage.local`);
          await browser.storage.local.set({ akey: "somevalue" });
          browser.test.log(`data stored in storage.local successfully`);
          break;
        case "has-storage": {
          browser.test.log(`checking data stored in storage.local`);
          const data = await browser.storage.local.get(["akey"]);
          browser.test.assertEq(
            data.akey,
            "somevalue",
            "Got storage.local data"
          );
          break;
        }
        default:
          browser.test.fail(`Unexpected test message: ${msg}`);
      }

      browser.test.sendMessage(`${msg}:done`);
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["unlimitedStorage", "storage"],
      browser_specific_settings: { gecko: { id } },
      version: "1",
    },
    useAddonManager: "permanent",
  });

  await extension.startup();

  const policy = WebExtensionPolicy.getByID(extension.id);
  const principal = policy.extension.principal;

  checkSitePermissions(
    principal,
    Services.perms.ALLOW_ACTION,
    "has been allowed"
  );

  extension.sendMessage("set-storage");
  await extension.awaitMessage("set-storage:done");
  extension.sendMessage("has-storage");
  await extension.awaitMessage("has-storage:done");

  // Simulate an update which do not require the unlimitedStorage permission.
  await extension.upgrade({
    background,
    manifest: {
      permissions: ["storage"],
      browser_specific_settings: { gecko: { id } },
      version: "2",
    },
    useAddonManager: "permanent",
  });

  const newPolicy = WebExtensionPolicy.getByID(extension.id);
  const newPrincipal = newPolicy.extension.principal;

  equal(
    principal.spec,
    newPrincipal.spec,
    "upgraded extension has the expected principal"
  );

  checkSitePermissions(
    principal,
    Services.perms.UNKNOWN_ACTION,
    "has been cleared"
  );

  // Verify that the previously stored data has not been
  // removed as a side effect of removing the unlimitedStorage
  // permission.
  extension.sendMessage("has-storage");
  await extension.awaitMessage("has-storage:done");

  await extension.unload();
});

add_task(async function test_unlimitedStorage_origin_attributes() {
  Services.prefs.setBoolPref("privacy.firstparty.isolate", true);

  const id = "test-unlimitedStorage-origin-attributes@mozilla";

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["unlimitedStorage"],
      browser_specific_settings: { gecko: { id } },
    },
  });

  await extension.startup();

  let policy = WebExtensionPolicy.getByID(extension.id);
  let principal = policy.extension.principal;

  ok(
    !principal.firstPartyDomain,
    "extension principal has no firstPartyDomain"
  );

  let perm = Services.perms.testExactPermissionFromPrincipal(
    principal,
    "persistent-storage"
  );
  equal(
    perm,
    Services.perms.ALLOW_ACTION,
    "Should have the correct permission without OAs"
  );

  await extension.unload();

  Services.prefs.clearUserPref("privacy.firstparty.isolate");
});