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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
let testRules = [
// Top-level cookie rule.
{
id: "87815b2d-a840-4155-8713-f8a26d1f483a",
click: {},
cookies: {
optIn: [
{
name: "foo",
value: "bar",
},
],
},
domains: [TEST_DOMAIN_B],
},
// Child click rule.
{
id: "d42bbaee-f96e-47e7-8e81-efc642518e97",
click: {
optOut: "#optOutBtn",
presence: "#cookieBanner",
runContext: "child",
},
cookies: {},
domains: [TEST_DOMAIN_C],
},
// Top level click rule.
{
id: "19dd1f52-f3e6-4a24-a926-d77f553d1b15",
click: {
optOut: "#optOutBtn",
presence: "#cookieBanner",
},
cookies: {},
domains: [TEST_DOMAIN_A],
},
];
/**
* Insert an iframe and wait for it to load.
* @param {BrowsingContext} parentBC - The BC the frame to insert under.
* @param {string} uri - The URI to load in the frame.
* @returns {Promise} - A Promise which resolves once the frame has loaded.
*/
function insertIframe(parentBC, uri) {
return SpecialPowers.spawn(parentBC, [uri], async testURL => {
let iframe = content.document.createElement("iframe");
iframe.src = testURL;
content.document.body.appendChild(iframe);
await ContentTaskUtils.waitForEvent(iframe, "load");
return iframe.browsingContext;
});
}
add_setup(async function() {
// Enable the service and insert the test rules. We only test
// MODE_REJECT_OR_ACCEPT here as the other modes are covered by other tests
// already and hasRuleForBrowsingContextTree mostly shares logic with other
// service getters.
await SpecialPowers.pushPrefEnv({
set: [
[
"cookiebanners.service.mode",
Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
],
["cookiebanners.listService.testSkipRemoteSettings", true],
["cookiebanners.listService.testRules", JSON.stringify(testRules)],
],
});
// Ensure the test rules have been applied before the first test starts.
Services.cookieBanners.resetRules();
});
add_task(async function test_unsupported() {
let unsupportedURIs = {
"about:preferences": /NS_ERROR_FAILURE/,
"about:blank": false,
};
for (let [key, value] of Object.entries(unsupportedURIs)) {
await BrowserTestUtils.withNewTab(key, async browser => {
if (typeof value == "object") {
// It's an error code.
Assert.throws(
() => {
Services.cookieBanners.hasRuleForBrowsingContextTree(
browser.browsingContext
);
},
value,
`Should throw ${value} for hasRuleForBrowsingContextTree call for '${key}'.`
);
} else {
is(
Services.cookieBanners.hasRuleForBrowsingContextTree(
browser.browsingContext
),
value,
`Should return expected value for hasRuleForBrowsingContextTree for '${key}'`
);
}
});
}
});
add_task(async function test_hasRuleForBCTree() {
info("Test with top level A");
await BrowserTestUtils.withNewTab(TEST_ORIGIN_A, async browser => {
let bcTop = browser.browsingContext;
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should have rule when called with top BC for A"
);
info("inserting frame with TEST_ORIGIN_A");
let bcChildA = await insertIframe(bcTop, TEST_ORIGIN_A);
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should still have rule when called with top BC for A."
);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildA),
"Should not have rule when called with child BC for A, because A has no child click-rule."
);
});
info("Test with top level C");
await BrowserTestUtils.withNewTab(TEST_ORIGIN_C, async browser => {
let bcTop = browser.browsingContext;
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should have no rule when called with top BC for C, because C only has a child click rule."
);
info("inserting frame with TEST_ORIGIN_C");
let bcChildC = await insertIframe(bcTop, TEST_ORIGIN_C);
info("inserting unrelated frames");
await insertIframe(bcTop, "https://itisatracker.org");
await insertIframe(bcChildC, "https://itisatracker.org");
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should have rule when called with top BC for C, because frame C has a child click rule."
);
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildC),
"Should have rule when called with child BC for C, because it has a child click rule."
);
});
info("Test with unrelated top level");
await BrowserTestUtils.withNewTab("http://mochi.test:8888", async browser => {
let bcTop = browser.browsingContext;
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should not have rule for unrelated site."
);
info("inserting frame with TEST_ORIGIN_A");
let bcChildA = await insertIframe(bcTop, TEST_ORIGIN_A);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should still have no rule when called with top BC for A, because click rule for A only applies top-level."
);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildA),
"Should have no rule when called with child BC for A."
);
info("inserting frame with TEST_ORIGIN_B");
let bcChildB = await insertIframe(bcTop, TEST_ORIGIN_B);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should still have no rule when called with top BC for A, because cookie rule for B only applies top-level."
);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildA),
"Should have no rule when called with child BC for A."
);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildB),
"Should have no rule when called with child BC for B."
);
info("inserting nested frame with TEST_ORIGIN_C");
let bcChildC = await insertIframe(bcChildB, TEST_ORIGIN_C);
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcTop),
"Should have rule when called with top level BC because rule for nested iframe C applies."
);
ok(
!Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildA),
"Should have no rule when called with child BC for A."
);
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildB),
"Should have rule when called with child BC for B, because C rule for nested iframe C applies."
);
ok(
Services.cookieBanners.hasRuleForBrowsingContextTree(bcChildC),
"Should have rule when called with child BC for C, because C rule for nested iframe C applies."
);
});
});
|