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

add_task(async function setup_cookies() {
  let extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    async background() {
      const url = "http://example.com/";
      const name = "dummyname";
      await browser.cookies.set({ url, name, value: "from_setup:normal" });
      await browser.cookies.set({
        url,
        name,
        value: "from_setup:private",
        storeId: "firefox-private",
      });
      await browser.cookies.set({
        url,
        name,
        value: "from_setup:container",
        storeId: "firefox-container-1",
      });
      browser.test.sendMessage("setup_done");
    },
    manifest: {
      permissions: ["cookies", "http://example.com/"],
    },
  });
  await extension.startup();
  await extension.awaitMessage("setup_done");
  await extension.unload();
});

add_task(async function test_error_messages() {
  async function background() {
    const url = "http://example.com/";
    const name = "dummyname";
    // Shorthands to minimize boilerplate.
    const set = d => browser.cookies.set({ url, name, value: "x", ...d });
    const remove = d => browser.cookies.remove({ url, name, ...d });
    const get = d => browser.cookies.get({ url, name, ...d });
    const getAll = d => browser.cookies.getAll(d);

    // Host permission permission missing.
    await browser.test.assertRejects(
      set({}),
      /^Permission denied to set cookie \{.*\}$/,
      "cookies.set without host permissions rejects with error"
    );
    browser.test.assertEq(
      null,
      await remove({}),
      "cookies.remove without host permissions does not remove any cookies"
    );
    browser.test.assertEq(
      null,
      await get({}),
      "cookies.get without host permissions does not match any cookies"
    );
    browser.test.assertEq(
      "[]",
      JSON.stringify(await getAll({})),
      "cookies.getAll without host permissions does not match any cookies"
    );

    // Private browsing cookies without access to private browsing mode.
    await browser.test.assertRejects(
      set({ storeId: "firefox-private" }),
      "Extension disallowed access to the private cookies storeId.",
      "cookies.set cannot modify private cookies without permission"
    );
    await browser.test.assertRejects(
      remove({ storeId: "firefox-private" }),
      "Extension disallowed access to the private cookies storeId.",
      "cookies.remove cannot modify private cookies without permission"
    );
    await browser.test.assertRejects(
      get({ storeId: "firefox-private" }),
      "Extension disallowed access to the private cookies storeId.",
      "cookies.get cannot read private cookies without permission"
    );
    await browser.test.assertRejects(
      getAll({ storeId: "firefox-private" }),
      "Extension disallowed access to the private cookies storeId.",
      "cookies.getAll cannot read private cookies without permission"
    );

    // On Android, any firefox-container-... is treated as valid, so it doesn't
    // result in an error. However, because the test extension does not have
    // any host permissions, it will fail with an error any way (but a
    // different one than expected).
    // TODO bug 1743616: Fix implementation and this test.
    const kErrorInvalidContainer = navigator.userAgent.includes("Android")
      ? /Permission denied to set cookie/
      : `Invalid cookie store id: "firefox-container-99"`;

    // Invalid storeId.
    await browser.test.assertRejects(
      set({ storeId: "firefox-container-99" }),
      kErrorInvalidContainer,
      "cookies.set with invalid storeId (non-existent container)"
    );

    await browser.test.assertRejects(
      set({ storeId: "0" }),
      `Invalid cookie store id: "0"`,
      "cookies.set with invalid storeId (format not recognized)"
    );

    for (let method of [remove, get, getAll]) {
      let resultWithInvalidStoreId = method == getAll ? [] : null;
      browser.test.assertEq(
        JSON.stringify(await method({ storeId: "firefox-container-99" })),
        JSON.stringify(resultWithInvalidStoreId),
        `cookies.${method.name} with invalid storeId (non-existent container)`
      );

      browser.test.assertEq(
        JSON.stringify(await method({ storeId: "0" })),
        JSON.stringify(resultWithInvalidStoreId),
        `cookies.${method.name} with invalid storeId (format not recognized)`
      );
    }

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

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["cookies"],
    },
  });
  await extension.startup();
  await extension.awaitMessage("test_done");
  await extension.unload();
});

add_task(async function expected_cookies_at_end_of_test() {
  let extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    async background() {
      async function checkCookie(storeId, value) {
        let cookies = await browser.cookies.getAll({ storeId });
        let index = cookies.findIndex(c => c.value === value);
        browser.test.assertTrue(index !== -1, `Found cookie: ${value}`);
        if (index >= 0) {
          cookies.splice(index, 1);
        }
        browser.test.assertEq(
          "[]",
          JSON.stringify(cookies),
          `No more cookies left in cookieStoreId=${storeId}`
        );
      }
      // Added in setup.
      await checkCookie("firefox-default", "from_setup:normal");
      await checkCookie("firefox-private", "from_setup:private");
      await checkCookie("firefox-container-1", "from_setup:container");
      browser.test.sendMessage("final_check_done");
    },
    manifest: {
      permissions: ["cookies", "<all_urls>"],
    },
  });
  await extension.startup();
  await extension.awaitMessage("final_check_done");
  await extension.unload();
});