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

add_setup(async function () {
  // make sure userContext is enabled.
  return SpecialPowers.pushPrefEnv({
    set: [["privacy.userContext.enabled", true]],
  });
});

add_task(async function () {
  info("Start testing tabs.create with cookieStoreId");

  let testCases = [
    // No private window
    {
      privateTab: false,
      cookieStoreId: null,
      success: true,
      expectedCookieStoreId: "firefox-default",
    },
    {
      privateTab: false,
      cookieStoreId: "firefox-default",
      success: true,
      expectedCookieStoreId: "firefox-default",
    },
    {
      privateTab: false,
      cookieStoreId: "firefox-container-1",
      success: true,
      expectedCookieStoreId: "firefox-container-1",
    },
    {
      privateTab: false,
      cookieStoreId: "firefox-container-2",
      success: true,
      expectedCookieStoreId: "firefox-container-2",
    },
    {
      privateTab: false,
      cookieStoreId: "firefox-container-42",
      failure: "exist",
    },
    {
      privateTab: false,
      cookieStoreId: "firefox-private",
      failure: "defaultToPrivate",
    },
    { privateTab: false, cookieStoreId: "wow", failure: "illegal" },

    // Private window
    {
      privateTab: true,
      cookieStoreId: null,
      success: true,
      expectedCookieStoreId: "firefox-private",
    },
    {
      privateTab: true,
      cookieStoreId: "firefox-private",
      success: true,
      expectedCookieStoreId: "firefox-private",
    },
    {
      privateTab: true,
      cookieStoreId: "firefox-default",
      failure: "privateToDefault",
    },
    {
      privateTab: true,
      cookieStoreId: "firefox-container-1",
      failure: "privateToDefault",
    },
    { privateTab: true, cookieStoreId: "wow", failure: "illegal" },
  ];

  let extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    manifest: {
      permissions: ["tabs", "cookies"],
    },

    background: function () {
      function testTab(data, tab) {
        browser.test.assertTrue(data.success, "we want a success");
        browser.test.assertTrue(!!tab, "we have a tab");
        browser.test.assertEq(
          data.expectedCookieStoreId,
          tab.cookieStoreId,
          "tab should have the correct cookieStoreId"
        );
      }

      async function runTest(data) {
        try {
          // Tab Creation
          let tab;
          try {
            tab = await browser.tabs.create({
              windowId: data.privateTab
                ? this.privateWindowId
                : this.defaultWindowId,
              cookieStoreId: data.cookieStoreId,
            });

            browser.test.assertTrue(!data.failure, "we want a success");
          } catch (error) {
            browser.test.assertTrue(!!data.failure, "we want a failure");

            if (data.failure == "illegal") {
              browser.test.assertEq(
                `Illegal cookieStoreId: ${data.cookieStoreId}`,
                error.message,
                "runtime.lastError should report the expected error message"
              );
            } else if (data.failure == "defaultToPrivate") {
              browser.test.assertEq(
                "Illegal to set private cookieStoreId in a non-private window",
                error.message,
                "runtime.lastError should report the expected error message"
              );
            } else if (data.failure == "privateToDefault") {
              browser.test.assertEq(
                "Illegal to set non-private cookieStoreId in a private window",
                error.message,
                "runtime.lastError should report the expected error message"
              );
            } else if (data.failure == "exist") {
              browser.test.assertEq(
                `No cookie store exists with ID ${data.cookieStoreId}`,
                error.message,
                "runtime.lastError should report the expected error message"
              );
            } else {
              browser.test.fail("The test is broken");
            }

            browser.test.sendMessage("test-done");
            return;
          }

          // Tests for tab creation
          testTab(data, tab);

          {
            // Tests for tab querying
            let [tab] = await browser.tabs.query({
              windowId: data.privateTab
                ? this.privateWindowId
                : this.defaultWindowId,
              cookieStoreId: data.cookieStoreId,
            });

            browser.test.assertTrue(tab != undefined, "Tab found!");
            testTab(data, tab);
          }

          let stores = await browser.cookies.getAllCookieStores();

          let store = stores.find(store => store.id === tab.cookieStoreId);
          browser.test.assertTrue(!!store, "We have a store for this tab.");
          browser.test.assertTrue(
            store.tabIds.includes(tab.id),
            "tabIds includes this tab."
          );

          await browser.tabs.remove(tab.id);

          browser.test.sendMessage("test-done");
        } catch (e) {
          browser.test.fail("An exception has been thrown");
        }
      }

      async function initialize() {
        let win = await browser.windows.create({ incognito: true });
        this.privateWindowId = win.id;

        win = await browser.windows.create({ incognito: false });
        this.defaultWindowId = win.id;

        browser.test.sendMessage("ready");
      }

      async function shutdown() {
        await browser.windows.remove(this.privateWindowId);
        await browser.windows.remove(this.defaultWindowId);
        browser.test.sendMessage("gone");
      }

      // Waiting for messages
      browser.test.onMessage.addListener((msg, data) => {
        if (msg == "be-ready") {
          initialize();
        } else if (msg == "test") {
          runTest(data);
        } else {
          browser.test.assertTrue("finish", msg, "Shutting down");
          shutdown();
        }
      });
    },
  });

  await extension.startup();

  info("Tests must be ready...");
  extension.sendMessage("be-ready");
  await extension.awaitMessage("ready");
  info("Tests are ready to run!");

  for (let test of testCases) {
    info(`test tab.create with cookieStoreId: "${test.cookieStoreId}"`);
    extension.sendMessage("test", test);
    await extension.awaitMessage("test-done");
  }

  info("Waiting for shutting down...");
  extension.sendMessage("finish");
  await extension.awaitMessage("gone");

  await extension.unload();
});

add_task(async function userContext_disabled() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.userContext.enabled", false]],
  });
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs", "cookies"],
    },
    async background() {
      await browser.test.assertRejects(
        browser.tabs.create({ cookieStoreId: "firefox-container-1" }),
        /Contextual identities are currently disabled/,
        "should refuse to open container tab when contextual identities are disabled"
      );
      browser.test.sendMessage("done");
    },
  });
  await extension.startup();
  await extension.awaitMessage("done");
  await extension.unload();
  await SpecialPowers.popPrefEnv();
});

add_task(async function tabs_query_cookiestoreid_nocookiepermission() {
  let extension = ExtensionTestUtils.loadExtension({
    async background() {
      let tab = await browser.tabs.create({});
      browser.test.assertEq(
        "firefox-default",
        tab.cookieStoreId,
        "Expecting cookieStoreId for new tab"
      );
      let query = await browser.tabs.query({
        index: tab.index,
        cookieStoreId: tab.cookieStoreId,
      });
      browser.test.assertEq(
        "firefox-default",
        query[0].cookieStoreId,
        "Expecting cookieStoreId for new tab through browser.tabs.query"
      );
      await browser.tabs.remove(tab.id);
      browser.test.sendMessage("done");
    },
  });
  await extension.startup();
  await extension.awaitMessage("done");
  await extension.unload();
});

add_task(async function tabs_query_multiple_cookiestoreId() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["cookies"],
    },

    async background() {
      let tab1 = await browser.tabs.create({
        cookieStoreId: "firefox-container-1",
      });
      browser.test.log(`Tab created for cookieStoreId:${tab1.cookieStoreId}`);

      let tab2 = await browser.tabs.create({
        cookieStoreId: "firefox-container-2",
      });
      browser.test.log(`Tab created for cookieStoreId:${tab2.cookieStoreId}`);

      let tab3 = await browser.tabs.create({
        cookieStoreId: "firefox-container-3",
      });
      browser.test.log(`Tab created for cookieStoreId:${tab3.cookieStoreId}`);

      let tabs = await browser.tabs.query({
        cookieStoreId: ["firefox-container-1", "firefox-container-2"],
      });

      browser.test.assertEq(
        2,
        tabs.length,
        "Expecting tabs for firefox-container-1 and firefox-container-2"
      );

      browser.test.assertEq(
        "firefox-container-1",
        tabs[0].cookieStoreId,
        "Expecting tab for firefox-container-1 cookieStoreId"
      );

      browser.test.assertEq(
        "firefox-container-2",
        tabs[1].cookieStoreId,
        "Expecting tab forfirefox-container-2 cookieStoreId"
      );

      await browser.tabs.remove([tab1.id, tab2.id, tab3.id]);
      browser.test.sendMessage("test-done");
    },
  });
  await extension.startup();
  await extension.awaitMessage("test-done");
  await extension.unload();
});