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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
const BEHAVIOR_ACCEPT = 0;
const BEHAVIOR_REJECT_FOREIGN = 1;
const BEHAVIOR_REJECT = 2;
const BEHAVIOR_LIMIT_FOREIGN = 3;
const kPrefName = "network.cookie.cookieBehavior";
// Check if we are in frame, and declare ok and finishTest appropriately
const inFrame = ("" + location).match(/frame/);
if (inFrame) {
ok = function (a, message) {
if (!a) {
parent.postMessage("FAILURE: " + message, "http://mochi.test:8888");
} else {
parent.postMessage(message, "http://mochi.test:8888");
}
};
finishTest = function () {
parent.postMessage("done", "http://mochi.test:8888");
};
} else {
finishTest = function () {
SimpleTest.finish();
};
}
function setCookieBehavior(behavior) {
return SpecialPowers.pushPrefEnv({ set: [[kPrefName, behavior]] });
}
function runIFrame(url) {
return new Promise((resolve, reject) => {
function onMessage(e) {
if (e.data == "done") {
resolve();
window.removeEventListener("message", onMessage);
return;
}
const isFail = e.data.match(/^FAILURE/);
ok(!isFail, e.data + " (IFRAME = " + url + ")");
if (isFail) {
reject(e);
}
}
window.addEventListener("message", onMessage);
document.querySelector("iframe").src = url;
});
}
function runWorker(url) {
return new Promise((resolve, reject) => {
var worker = new Worker(url);
worker.addEventListener("message", function (e) {
if (e.data == "done") {
resolve();
return;
}
ok(!e.data.match(/^FAILURE/), e.data + " (WORKER = " + url + ")");
});
worker.addEventListener("error", function (e) {
ok(false, e.data + " (WORKER = " + url + ")");
reject(e);
});
});
}
function chromePower(allowed, blockSessionStorage) {
// localStorage is affected by storage policy.
try {
SpecialPowers.wrap(window).localStorage.getItem("X");
ok(allowed, "getting localStorage from chrome didn't throw");
} catch (e) {
ok(!allowed, "getting localStorage from chrome threw");
}
// sessionStorage is not. See bug 1183968.
try {
SpecialPowers.wrap(window).sessionStorage.getItem("X");
ok(!blockSessionStorage, "getting sessionStorage from chrome didn't throw");
} catch (e) {
ok(blockSessionStorage, "getting sessionStorage from chrome threw");
}
// indexedDB is affected by storage policy.
try {
SpecialPowers.wrap(window).indexedDB;
ok(allowed, "getting indexedDB from chrome didn't throw");
} catch (e) {
ok(!allowed, "getting indexedDB from chrome threw");
}
// Same with caches, along with the additional https-only requirement.
try {
var shouldResolve = allowed && location.protocol == "https:";
var promise = SpecialPowers.wrap(window).caches.keys();
ok(true, "getting caches from chrome should never throw");
return new Promise((resolve, reject) => {
promise.then(
function () {
ok(shouldResolve, "The promise was resolved for chrome");
resolve();
},
function (e) {
ok(!shouldResolve, "The promise was rejected for chrome: " + e);
resolve();
}
);
});
} catch (e) {
ok(false, "getting caches from chrome threw");
return undefined;
}
}
function storageAllowed() {
try {
localStorage.getItem("X");
ok(true, "getting localStorage didn't throw");
} catch (e) {
ok(false, "getting localStorage should not throw");
}
try {
sessionStorage.getItem("X");
ok(true, "getting sessionStorage didn't throw");
} catch (e) {
ok(false, "getting sessionStorage should not throw");
}
try {
indexedDB;
ok(true, "getting indexedDB didn't throw");
} catch (e) {
ok(false, "getting indexedDB should not throw");
}
const dbName = "db";
try {
var promise = caches.keys();
ok(true, "getting caches didn't throw");
return new Promise((resolve, reject) => {
const checkCacheKeys = () => {
promise.then(
() => {
ok(location.protocol == "https:", "The promise was not rejected");
resolve();
},
() => {
ok(
location.protocol !== "https:",
"The promise should not have been rejected"
);
resolve();
}
);
};
const checkDeleteDbAndTheRest = dbs => {
ok(
dbs.some(elem => elem.name === dbName),
"Expected database should be found"
);
const end = indexedDB.deleteDatabase(dbName);
end.onsuccess = checkCacheKeys;
end.onerror = err => {
ok(false, "querying indexedDB databases should not throw");
reject(err);
};
};
const checkDatabasesAndTheRest = () => {
indexedDB
.databases()
.then(checkDeleteDbAndTheRest)
.catch(err => {
ok(false, "deleting an indexedDB database should not throw");
reject(err);
});
};
const begin = indexedDB.open(dbName);
begin.onsuccess = checkDatabasesAndTheRest;
begin.onerror = err => {
ok(false, "opening an indexedDB database should not throw");
reject(err);
};
});
} catch (e) {
ok(location.protocol !== "https:", "getting caches should not have thrown");
return Promise.resolve();
}
}
function storagePrevented() {
try {
localStorage.getItem("X");
ok(false, "getting localStorage should have thrown");
} catch (e) {
ok(true, "getting localStorage threw");
}
if (location.hash == "#thirdparty") {
// No matter what the user's preferences are, we don't block
// sessionStorage in 3rd-party iframes. We do block them everywhere
// else however.
try {
sessionStorage.getItem("X");
ok(true, "getting sessionStorage didn't throw");
} catch (e) {
ok(false, "getting sessionStorage should not have thrown");
}
} else {
try {
sessionStorage.getItem("X");
ok(false, "getting sessionStorage should have thrown");
} catch (e) {
ok(true, "getting sessionStorage threw");
}
}
try {
indexedDB;
ok(true, "getting indexedDB didn't throw");
} catch (e) {
ok(false, "getting indexedDB should not have thrown");
}
const dbName = "album";
try {
indexedDB.open(dbName);
ok(false, "opening an indexedDB database didn't throw");
} catch (e) {
ok(true, "opening an indexedDB database threw");
ok(
e.name == "SecurityError",
"opening indexedDB database emitted a security error"
);
}
// Note: Security error is expected to be thrown synchronously.
indexedDB.databases().then(
() => {
ok(false, "querying indexedDB databases didn't reject");
},
e => {
ok(true, "querying indexedDB databases rejected");
ok(
e.name == "SecurityError",
"querying indexedDB databases emitted a security error"
);
}
);
try {
indexedDB.deleteDatabase(dbName);
ok(false, "deleting an indexedDB database didn't throw");
} catch (e) {
ok(true, "deleting an indexedDB database threw");
ok(
e.name == "SecurityError",
"deleting indexedDB database emitted a security error"
);
}
try {
var promise = caches.keys();
ok(true, "getting caches didn't throw");
return new Promise((resolve, reject) => {
promise.then(
function () {
ok(false, "The promise should have rejected");
resolve();
},
function () {
ok(true, "The promise was rejected");
resolve();
}
);
});
} catch (e) {
ok(location.protocol !== "https:", "getting caches should not have thrown");
return Promise.resolve();
}
}
function task(fn) {
if (!inFrame) {
SimpleTest.waitForExplicitFinish();
}
var gen = fn();
function next_step(val, e) {
var it;
try {
if (typeof e !== "undefined") {
it = gen.throw(e);
} else {
it = gen.next(val);
}
} catch (e) {
ok(false, "An error was thrown while stepping: " + e);
ok(false, "Stack: " + e.stack);
finishTest();
}
if (it.done) {
finishTest();
return;
}
it.value.then(next_step, e => next_step(null, e));
}
if (!gen.then) {
next_step();
} else {
gen.then(finishTest, e => {
ok(false, "An error was thrown while stepping: " + e);
ok(false, "Stack: " + e.stack);
finishTest();
});
}
}
// The test will run on a separate window in order to apply the new cookie jar settings.
async function runTestInWindow(test) {
let w = window.open("window_storagePermissions.html");
await new Promise(resolve => {
w.onload = e => {
resolve();
};
});
return new Promise((resolve, reject) => {
onmessage = e => {
if (!e.data.type) {
w.postMessage("FAILURE: " + e.data, document.referrer);
ok(false, "No error data type");
reject(e);
return;
}
if (e.data.type == "finish") {
w.close();
resolve();
return;
}
if (e.data.type == "check") {
const payload = e.data.msg ? e.data.msg : e.data;
ok(e.data.test, payload);
const isFail = payload.match(/^FAILURE/) || !e.data.test;
if (isFail) {
w.postMessage("FAILURE: " + e.data, document.referrer);
ok(false, payload);
w.close();
reject(e);
}
return;
}
ok(false, "Unknown message");
};
w.postMessage(test.toString(), "*");
});
}
var thirdparty = "https://example.com/tests/dom/tests/mochitest/general/";
|