summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/xpcshell/test_clear_blocked_cookies.js
blob: 571ae95a1be8d1eaf09dd06400c716ecf6a3a8d5 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

const HOSTNAME_DOMAIN = "browser_policy_clear_blocked_cookies.com";
const ORIGIN_DOMAIN = "browser_policy_clear_blocked_cookies.org";

add_task(async function setup() {
  const expiry = Date.now() + 24 * 60 * 60;
  Services.cookies.add(
    HOSTNAME_DOMAIN,
    "/",
    "secure",
    "true",
    true,
    false,
    false,
    expiry,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTPS
  );
  Services.cookies.add(
    HOSTNAME_DOMAIN,
    "/",
    "insecure",
    "true",
    false,
    false,
    false,
    expiry,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTP
  );
  Services.cookies.add(
    ORIGIN_DOMAIN,
    "/",
    "secure",
    "true",
    true,
    false,
    false,
    expiry,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTPS
  );
  Services.cookies.add(
    ORIGIN_DOMAIN,
    "/",
    "insecure",
    "true",
    false,
    false,
    false,
    expiry,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTP
  );
  Services.cookies.add(
    "example.net",
    "/",
    "secure",
    "true",
    true,
    false,
    false,
    expiry,
    {},
    Ci.nsICookie.SAMESITE_NONE,
    Ci.nsICookie.SCHEME_HTTPS
  );
  await setupPolicyEngineWithJson({
    policies: {
      Cookies: {
        Block: [`http://${HOSTNAME_DOMAIN}`, `https://${ORIGIN_DOMAIN}:8080`],
      },
    },
  });
});

function retrieve_all_cookies(host) {
  const values = [];
  for (let cookie of Services.cookies.getCookiesFromHost(host, {})) {
    values.push({
      host: cookie.host,
      name: cookie.name,
      path: cookie.path,
    });
  }
  return values;
}

add_task(async function test_cookies_for_blocked_sites_cleared() {
  const cookies = {
    hostname: retrieve_all_cookies(HOSTNAME_DOMAIN),
    origin: retrieve_all_cookies(ORIGIN_DOMAIN),
    keep: retrieve_all_cookies("example.net"),
  };
  const expected = {
    hostname: [],
    origin: [],
    keep: [{ host: "example.net", name: "secure", path: "/" }],
  };
  equal(
    JSON.stringify(cookies),
    JSON.stringify(expected),
    "All stored cookies for blocked origins should be cleared"
  );
});

add_task(function teardown() {
  for (let host of [HOSTNAME_DOMAIN, ORIGIN_DOMAIN, "example.net"]) {
    Services.cookies.removeCookiesWithOriginAttributes("{}", host);
  }
});