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

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

// In this test, we want to check the behavior of extensions without private
// browsing access. Privileged add-ons automatically have private browsing
// access, so make sure that the test add-ons are not privileged.
AddonTestUtils.usePrivilegedSignatures = false;

add_task(async function setup() {
  await ExtensionTestUtils.startAddonManager();

  Services.prefs.setBoolPref("extensions.eventPages.enabled", true);
});

function createTestExtension({ privateAllowed }) {
  return ExtensionTestUtils.loadExtension({
    incognitoOverride: privateAllowed ? "spanning" : null,
    manifest: {
      permissions: ["cookies"],
      host_permissions: ["https://example.com/"],
      background: { persistent: false },
    },
    background() {
      browser.cookies.onChanged.addListener(changeInfo => {
        browser.test.sendMessage("cookie-event", changeInfo);
      });
    },
  });
}

function addAndRemoveCookie({ isPrivate }) {
  const cookie = {
    name: "cookname",
    value: "cookvalue",
    domain: "example.com",
    hostOnly: true,
    path: "/",
    secure: true,
    httpOnly: false,
    sameSite: "lax",
    session: false,
    firstPartyDomain: "",
    partitionKey: null,
    expirationDate: Date.now() + 3600000,
    storeId: isPrivate ? "firefox-private" : "firefox-default",
  };
  const originAttributes = { privateBrowsingId: isPrivate ? 1 : 0 };
  Services.cookies.add(
    cookie.domain,
    cookie.path,
    cookie.name,
    cookie.value,
    cookie.secure,
    cookie.httpOnly,
    cookie.session,
    cookie.expirationDate,
    originAttributes,
    Ci.nsICookie.SAMESITE_LAX,
    Ci.nsICookie.SCHEME_HTTPS
  );
  Services.cookies.remove(
    cookie.domain,
    cookie.name,
    cookie.path,
    originAttributes
  );
  return cookie;
}

add_task(async function test_onChanged_event_page() {
  let nonPrivateExtension = createTestExtension({ privateAllowed: false });
  let privateExtension = createTestExtension({ privateAllowed: true });
  await privateExtension.startup();
  await nonPrivateExtension.startup();
  assertPersistentListeners(privateExtension, "cookies", "onChanged", {
    primed: false,
  });
  assertPersistentListeners(nonPrivateExtension, "cookies", "onChanged", {
    primed: false,
  });

  // Suspend both event pages.
  await privateExtension.terminateBackground();
  assertPersistentListeners(privateExtension, "cookies", "onChanged", {
    primed: true,
  });
  await nonPrivateExtension.terminateBackground();
  assertPersistentListeners(nonPrivateExtension, "cookies", "onChanged", {
    primed: true,
  });

  // Modifying a private cookie should wake up the private extension, but not
  // the other one that does not have access to private browsing data.
  let privateCookie = addAndRemoveCookie({ isPrivate: true });

  Assert.deepEqual(
    await privateExtension.awaitMessage("cookie-event"),
    { removed: false, cookie: privateCookie, cause: "explicit" },
    "cookies.onChanged for private cookie creation"
  );
  Assert.deepEqual(
    await privateExtension.awaitMessage("cookie-event"),
    { removed: true, cookie: privateCookie, cause: "explicit" },
    "cookies.onChanged for private cookie removal"
  );
  // Private extension should have awakened...
  assertPersistentListeners(privateExtension, "cookies", "onChanged", {
    primed: false,
  });
  // ... but the non-private extension should still be sound asleep.
  assertPersistentListeners(nonPrivateExtension, "cookies", "onChanged", {
    primed: true,
  });

  // A non-private cookie modification should notify both extensions.
  let nonPrivateCookie = addAndRemoveCookie({ isPrivate: false });
  Assert.deepEqual(
    await privateExtension.awaitMessage("cookie-event"),
    { removed: false, cookie: nonPrivateCookie, cause: "explicit" },
    "cookies.onChanged for cookie creation in privateExtension"
  );
  Assert.deepEqual(
    await privateExtension.awaitMessage("cookie-event"),
    { removed: true, cookie: nonPrivateCookie, cause: "explicit" },
    "cookies.onChanged for cookie removal in privateExtension"
  );
  Assert.deepEqual(
    await nonPrivateExtension.awaitMessage("cookie-event"),
    { removed: false, cookie: nonPrivateCookie, cause: "explicit" },
    "cookies.onChanged for cookie creation in nonPrivateExtension"
  );
  Assert.deepEqual(
    await nonPrivateExtension.awaitMessage("cookie-event"),
    { removed: true, cookie: nonPrivateCookie, cause: "explicit" },
    "cookies.onChanged for cookie removal in nonPrivateCookie"
  );

  await privateExtension.unload();
  await nonPrivateExtension.unload();
});