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

// "Normal" cookie
const COOKIE_NORMAL = {
  host: "example.com",
  name: "test_cookie",
  path: "/",
  originAttributes: {},
};
// Private browsing cookie
const COOKIE_PRIVATE = {
  host: "example.net",
  name: "test_cookie",
  path: "/",
  originAttributes: {
    privateBrowsingId: 1,
  },
};
// "firefox-container-1" cookie
const COOKIE_CONTAINER = {
  host: "example.org",
  name: "test_cookie",
  path: "/",
  originAttributes: {
    userContextId: 1,
  },
};

function cookieExists(cookie) {
  return Services.cookies.cookieExists(
    cookie.host,
    cookie.path,
    cookie.name,
    cookie.originAttributes
  );
}

function addCookie(cookie) {
  const THE_FUTURE = Date.now() + 5 * 60;

  Services.cookies.add(
    cookie.host,
    cookie.path,
    cookie.name,
    "test",
    false,
    false,
    false,
    THE_FUTURE,
    cookie.originAttributes,
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTPS
  );

  ok(cookieExists(cookie), `Cookie ${cookie.name} was created.`);
}

async function setUpCookies() {
  Services.cookies.removeAll();

  addCookie(COOKIE_NORMAL);
  addCookie(COOKIE_PRIVATE);
  addCookie(COOKIE_CONTAINER);
}

add_task(async function testCookies() {
  Services.prefs.setBoolPref("privacy.userContext.enabled", true);

  function background() {
    browser.test.onMessage.addListener(async (msg, options) => {
      if (msg == "removeCookies") {
        await browser.browsingData.removeCookies(options);
      } else {
        await browser.browsingData.remove(options, { cookies: true });
      }
      browser.test.sendMessage("cookiesRemoved");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["browsingData"],
    },
  });

  async function testRemovalMethod(method) {
    // Clear only "normal"/default cookies.
    await setUpCookies();

    extension.sendMessage(method, { cookieStoreId: "firefox-default" });
    await extension.awaitMessage("cookiesRemoved");

    ok(!cookieExists(COOKIE_NORMAL), "Normal cookie was removed");
    ok(cookieExists(COOKIE_PRIVATE), "Private cookie was not removed");
    ok(cookieExists(COOKIE_CONTAINER), "Container cookie was not removed");

    // Clear container cookie
    await setUpCookies();

    extension.sendMessage(method, { cookieStoreId: "firefox-container-1" });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(cookieExists(COOKIE_PRIVATE), "Private cookie was not removed");
    ok(!cookieExists(COOKIE_CONTAINER), "Container cookie was removed");

    // Clear private cookie
    await setUpCookies();

    extension.sendMessage(method, { cookieStoreId: "firefox-private" });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(!cookieExists(COOKIE_PRIVATE), "Private cookie was removed");
    ok(cookieExists(COOKIE_CONTAINER), "Container cookie was not removed");

    // Clear container cookie with correct hostname
    await setUpCookies();

    extension.sendMessage(method, {
      cookieStoreId: "firefox-container-1",
      hostnames: ["example.org"],
    });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(cookieExists(COOKIE_PRIVATE), "Private cookie was not removed");
    ok(!cookieExists(COOKIE_CONTAINER), "Container cookie was removed");

    // Clear container cookie with incorrect hostname; nothing is removed
    await setUpCookies();

    extension.sendMessage(method, {
      cookieStoreId: "firefox-container-1",
      hostnames: ["example.com"],
    });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(cookieExists(COOKIE_PRIVATE), "Private cookie was not removed");
    ok(cookieExists(COOKIE_CONTAINER), "Container cookie was not removed");

    // Clear private cookie with correct hostname
    await setUpCookies();

    extension.sendMessage(method, {
      cookieStoreId: "firefox-private",
      hostnames: ["example.net"],
    });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(!cookieExists(COOKIE_PRIVATE), "Private cookie was removed");
    ok(cookieExists(COOKIE_CONTAINER), "Container cookie was not removed");

    // Clear private cookie with incorrect hostname; nothing is removed
    await setUpCookies();

    extension.sendMessage(method, {
      cookieStoreId: "firefox-private",
      hostnames: ["example.com"],
    });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(cookieExists(COOKIE_PRIVATE), "Private cookie was not removed");
    ok(cookieExists(COOKIE_CONTAINER), "Container cookie was not removed");

    // Clear private cookie by hostname
    await setUpCookies();

    extension.sendMessage(method, {
      hostnames: ["example.net"],
    });
    await extension.awaitMessage("cookiesRemoved");

    ok(cookieExists(COOKIE_NORMAL), "Normal cookie was not removed");
    ok(!cookieExists(COOKIE_PRIVATE), "Private cookie was removed");
    ok(cookieExists(COOKIE_CONTAINER), "Container cookie was not removed");
  }

  await extension.startup();

  await testRemovalMethod("removeCookies");
  await testRemovalMethod("remove");

  await extension.unload();
});