summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_restore_getCookiesWithOriginAttributes.js
blob: df79de9ca29180ae78ba146ff88c2189e2a6fe8a (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
/*
 * Bug 1334587 - A Test case for checking whether forgetting APIs are working for cookies.
 */

const CC = Components.Constructor;

const TEST_HOST = "example.com";
const TEST_URL =
  "http://" +
  TEST_HOST +
  "/browser/browser/components/contextualidentity/test/browser/";

const USER_CONTEXTS = ["default", "personal", "work"];

const DELETE_CONTEXT = 1;
const COOKIE_NAME = "userContextId";

//
// Support functions.
//

async function openTabInUserContext(uri, userContextId) {
  // Open the tab in the correct userContextId.
  let tab = BrowserTestUtils.addTab(gBrowser, uri, { userContextId });

  // Select tab and make sure its browser is focused.
  gBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);
  return { tab, browser };
}

function getCookiesForOA(host, userContextId) {
  return Services.cookies.getCookiesFromHost(host, { userContextId });
}

//
// Test functions.
//

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

function checkCookies(ignoreContext = null) {
  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    if (ignoreContext && userContextId === String(ignoreContext)) {
      continue;
    }
    let cookies = getCookiesForOA(TEST_HOST, userContextId);
    ok(cookies.length, "Cookies available");

    let foundCookie = cookies[0];
    is(foundCookie.name, COOKIE_NAME, "Check cookie name");
    is(foundCookie.value, USER_CONTEXTS[userContextId], "Check cookie value");
  }
}

function deleteCookies(onlyContext = null) {
  // Using getCookiesWithOriginAttributes() to get all cookies for a certain
  // domain by using the originAttributes pattern, and clear all these cookies.
  for (let cookie of Services.cookies.getCookiesWithOriginAttributes(
    JSON.stringify({}),
    TEST_HOST
  )) {
    if (!onlyContext || cookie.originAttributes.userContextId == onlyContext) {
      Services.cookies.remove(
        cookie.host,
        cookie.name,
        cookie.path,
        cookie.originAttributes
      );
    }
  }
}

add_task(async function test_cookie_getCookiesWithOriginAttributes() {
  let tabs = [];

  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    // Load the page in different contexts and set a cookie
    // which should only be visible in that context.
    let value = USER_CONTEXTS[userContextId];

    // Open our tab in the given user context.
    tabs[userContextId] = await openTabInUserContext(
      TEST_URL + "file_reflect_cookie_into_title.html?" + value,
      userContextId
    );

    // Close this tab.
    BrowserTestUtils.removeTab(tabs[userContextId].tab);
  }

  // Check that cookies have been set properly.
  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    let cookies = getCookiesForOA(TEST_HOST, userContextId);
    ok(cookies.length, "Cookies available");

    let foundCookie = cookies[0];
    is(foundCookie.name, COOKIE_NAME, "Check cookie name");
    is(foundCookie.value, USER_CONTEXTS[userContextId], "Check cookie value");
  }
  checkCookies();

  deleteCookies(DELETE_CONTEXT);

  checkCookies(DELETE_CONTEXT);

  deleteCookies();

  // Check that whether cookies has been cleared.
  for (let userContextId of Object.keys(USER_CONTEXTS)) {
    let cookies = getCookiesForOA(TEST_HOST, userContextId);
    ok(!cookies.length, "No Cookie should be here");
  }
});