summaryrefslogtreecommitdiffstats
path: root/dom/security/featurepolicy/test/mochitest/test_parser.html
blob: a8322f6e7d0cb39eb69f9ba70834e960e2b2aae3 (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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE HTML>
<html>
<head>
  <title>Test feature policy - parsing</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<iframe src="empty.html" id="ifr"></iframe>
<iframe src="https://example.org/tests/dom/security/featurePolicy/test/mochitest/empty.html" id="cross_ifr"></iframe>
<script type="text/javascript">

SimpleTest.waitForExplicitFinish();

const CROSS_ORIGIN = "https://example.org";

function test_document() {
  info("Checking document.featurePolicy");
  ok("featurePolicy" in document, "We have document.featurePolicy");

  ok(!document.featurePolicy.allowsFeature("foobar"), "Random feature");
  ok(!document.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

  ok(document.featurePolicy.allowsFeature("camera"), "Camera is allowed for self");
  ok(document.featurePolicy.allowsFeature("camera", "https://foo.bar"), "Camera is always allowed");
  let allowed = document.featurePolicy.getAllowlistForFeature("camera");
  is(allowed.length, 1, "Only 1 entry in allowlist for camera");
  is(allowed[0], "*", "allowlist is *");

  ok(document.featurePolicy.allowsFeature("geolocation"), "Geolocation is allowed for self");
  ok(document.featurePolicy.allowsFeature("geolocation", location.origin), "Geolocation is allowed for self");
  ok(!document.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
  allowed = document.featurePolicy.getAllowlistForFeature("geolocation");
  is(allowed.length, 1, "Only 1 entry in allowlist for geolocation");
  is(allowed[0], location.origin, "allowlist is self");

  ok(!document.featurePolicy.allowsFeature("microphone"), "Microphone is disabled for self");
  ok(!document.featurePolicy.allowsFeature("microphone", location.origin), "Microphone is disabled for self");
  ok(!document.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
  ok(document.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is allowed for example.com");
  ok(document.featurePolicy.allowsFeature("microphone", "https://example.org"), "Microphone is allowed for example.org");
  allowed = document.featurePolicy.getAllowlistForFeature("microphone");
  is(allowed.length, 0, "No allowlist for microphone");

  ok(!document.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
  ok(!document.featurePolicy.allowsFeature("vr", location.origin), "Vibrate is disabled for self");
  ok(!document.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
  allowed = document.featurePolicy.getAllowlistForFeature("vr");
  is(allowed.length, 0, "No allowlist for vr");

  allowed = document.featurePolicy.allowedFeatures();
  // microphone is disabled for this origin, vr is disabled everywhere.
  let camera = false;
  let geolocation = false;
  allowed.forEach(a => {
    if (a == "camera") camera = true;
    if (a == "geolocation") geolocation = true;
  });

  ok(camera, "Camera is always allowed");
  ok(geolocation, "Geolocation is allowed only for self");

  next();
}

function test_iframe_without_allow() {
  info("Checking HTMLIFrameElement.featurePolicy");
  let ifr = document.getElementById("ifr");
  ok("featurePolicy" in ifr, "HTMLIFrameElement.featurePolicy exists");

  ok(!ifr.featurePolicy.allowsFeature("foobar"), "Random feature");
  ok(!ifr.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

  ok(ifr.featurePolicy.allowsFeature("camera"), "Camera is allowed for self");
  ok(ifr.featurePolicy.allowsFeature("camera", location.origin), "Camera is allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("camera", "https://foo.bar"), "Camera is not allowed for a random URL");
  let allowed = ifr.featurePolicy.getAllowlistForFeature("camera");
  is(allowed.length, 1, "Only 1 entry in allowlist for camera");
  is(allowed[0], location.origin, "allowlist is 'self'");

  ok(ifr.featurePolicy.allowsFeature("geolocation"), "Geolocation is allowed for self");
  ok(ifr.featurePolicy.allowsFeature("geolocation", location.origin), "Geolocation is allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
  allowed = ifr.featurePolicy.getAllowlistForFeature("geolocation");
  is(allowed.length, 1, "Only 1 entry in allowlist for geolocation");
  is(allowed[0], location.origin, "allowlist is '*'");

  ok(!ifr.featurePolicy.allowsFeature("microphone"), "Microphone is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("microphone", location.origin), "Microphone is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is disabled for example.com");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://example.org"), "Microphone is disabled for example.org");
  allowed = ifr.featurePolicy.getAllowlistForFeature("microphone");
  is(allowed.length, 0, "No allowlist for microphone");

  ok(!ifr.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", location.origin), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
  allowed = ifr.featurePolicy.getAllowlistForFeature("vr");
  is(allowed.length, 0, "No allowlist for vr");

  ok(ifr.featurePolicy.allowedFeatures().includes("camera"), "Camera is allowed");
  ok(ifr.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is allowed");
  // microphone is disabled for this origin
  ok(!ifr.featurePolicy.allowedFeatures().includes("microphone"), "microphone is not allowed");
  // vr is disabled everywhere.
  ok(!ifr.featurePolicy.allowedFeatures().includes("vr"), "VR is not allowed");

  next();
}

function test_iframe_with_allow() {
  info("Checking HTMLIFrameElement.featurePolicy");
  let ifr = document.getElementById("ifr");
  ok("featurePolicy" in ifr, "HTMLIFrameElement.featurePolicy exists");

  ifr.setAttribute("allow", "camera 'none'");

  ok(!ifr.featurePolicy.allowsFeature("foobar"), "Random feature");
  ok(!ifr.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

  ok(!ifr.featurePolicy.allowsFeature("camera"), "Camera is not allowed");
  let allowed = ifr.featurePolicy.getAllowlistForFeature("camera");
  is(allowed.length, 0, "Camera has an empty allowlist");

  ok(ifr.featurePolicy.allowsFeature("geolocation"), "Geolocation is allowed for self");
  ok(ifr.featurePolicy.allowsFeature("geolocation", location.origin), "Geolocation is allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
  allowed = ifr.featurePolicy.getAllowlistForFeature("geolocation");
  is(allowed.length, 1, "Only 1 entry in allowlist for geolocation");
  is(allowed[0], location.origin, "allowlist is '*'");

  ok(!ifr.featurePolicy.allowsFeature("microphone"), "Microphone is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("microphone", location.origin), "Microphone is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is disabled for example.com");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://example.org"), "Microphone is disabled for example.org");
  allowed = ifr.featurePolicy.getAllowlistForFeature("microphone");
  is(allowed.length, 0, "No allowlist for microphone");

  ok(!ifr.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", location.origin), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
  allowed = ifr.featurePolicy.getAllowlistForFeature("vr");
  is(allowed.length, 0, "No allowlist for vr");

  ok(ifr.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is allowed only for self");

  next();
}

function test_iframe_contentDocument() {
  info("Checking iframe document.featurePolicy");

  let ifr = document.createElement("iframe");
  ifr.setAttribute("src", "empty.html");
  ifr.onload = function() {
    ok("featurePolicy" in ifr.contentDocument, "We have ifr.contentDocument.featurePolicy");

    ok(!ifr.contentDocument.featurePolicy.allowsFeature("foobar"), "Random feature");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

    ok(ifr.contentDocument.featurePolicy.allowsFeature("camera"), "Camera is allowed for self");
    ok(ifr.contentDocument.featurePolicy.allowsFeature("camera", location.origin), "Camera is allowed for self");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("camera", "https://foo.bar"), "Camera is allowed for self");
    let allowed = ifr.contentDocument.featurePolicy.getAllowlistForFeature("camera");
    is(allowed.length, 1, "Only 1 entry in allowlist for camera");
    is(allowed[0], location.origin, "allowlist is 'self'");

    ok(ifr.featurePolicy.allowsFeature("geolocation"), "Geolocation is allowed for self");
    ok(ifr.featurePolicy.allowsFeature("geolocation", location.origin), "Geolocation is allowed for self");
    ok(!ifr.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
    allowed = ifr.featurePolicy.getAllowlistForFeature("geolocation");
    is(allowed.length, 1, "Only 1 entry in allowlist for geolocation");
    is(allowed[0], location.origin, "allowlist is '*'");

    ok(!ifr.contentDocument.featurePolicy.allowsFeature("microphone"), "Microphone is disabled for self");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("microphone", location.origin), "Microphone is disabled for self");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is allowed for example.com");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("microphone", "https://example.org"), "Microphone is allowed for example.org");
    allowed = ifr.contentDocument.featurePolicy.getAllowlistForFeature("microphone");
    is(allowed.length, 0, "No allowlist for microphone");

    ok(!ifr.contentDocument.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("vr", location.origin), "Vibrate is disabled for self");
    ok(!ifr.contentDocument.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
    allowed = ifr.contentDocument.featurePolicy.getAllowlistForFeature("vr");
    is(allowed.length, 0, "No allowlist for vr");

    ok(ifr.contentDocument.featurePolicy.allowedFeatures().includes("camera"), "Camera is allowed");
    ok(ifr.contentDocument.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is allowed");
    // microphone is disabled for this origin
    ok(!ifr.contentDocument.featurePolicy.allowedFeatures().includes("microphone"), "Microphone is not allowed");
    // vr is disabled everywhere.
    ok(!ifr.contentDocument.featurePolicy.allowedFeatures().includes("vr"), "VR is not allowed");

    next();
  };
  document.body.appendChild(ifr);
}

function test_cross_iframe_without_allow() {
  info("Checking cross HTMLIFrameElement.featurePolicy no allow");
  let ifr = document.getElementById("cross_ifr");
  ok("featurePolicy" in ifr, "HTMLIFrameElement.featurePolicy exists");

  ok(!ifr.featurePolicy.allowsFeature("foobar"), "Random feature");
  ok(!ifr.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

  ok(ifr.featurePolicy.allowsFeature("camera"), "Camera is allowed for self");
  ok(ifr.featurePolicy.allowsFeature("camera", CROSS_ORIGIN), "Camera is allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("camera", "https://foo.bar"), "Camera is not allowed for a random URL");
  let allowed = ifr.featurePolicy.getAllowlistForFeature("camera");
  is(allowed.length, 1, "Only 1 entry in allowlist for camera");
  is(allowed[0], CROSS_ORIGIN, "allowlist is 'self'");

  ok(!ifr.featurePolicy.allowsFeature("geolocation"), "Geolocation is not allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("geolocation", CROSS_ORIGIN),
    "Geolocation is not allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
  allowed = ifr.featurePolicy.getAllowlistForFeature("geolocation");
  is(allowed.length, 0, "No allowlist for geolocation");

  ok(ifr.featurePolicy.allowsFeature("microphone"), "Microphone is enabled for self");
  ok(ifr.featurePolicy.allowsFeature("microphone", CROSS_ORIGIN), "Microphone is enabled for self");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is disabled for example.com");
  allowed = ifr.featurePolicy.getAllowlistForFeature("microphone");
  is(allowed.length, 1, "Only 1 entry in allowlist for microphone");
  is(allowed[0], CROSS_ORIGIN, "allowlist is self");

  ok(!ifr.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", CROSS_ORIGIN), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
  allowed = ifr.featurePolicy.getAllowlistForFeature("vr");
  is(allowed.length, 0, "No allowlist for vr");

  ok(ifr.featurePolicy.allowedFeatures().includes("camera"), "Camera is allowed");
  ok(!ifr.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is not allowed");
  // microphone is enabled for this origin
  ok(ifr.featurePolicy.allowedFeatures().includes("microphone"), "microphone is allowed");
  // vr is disabled everywhere.
  ok(!ifr.featurePolicy.allowedFeatures().includes("vr"), "VR is not allowed");

  next();
}

function test_cross_iframe_with_allow() {
  info("Checking cross HTMLIFrameElement.featurePolicy with allow");
  let ifr = document.getElementById("cross_ifr");
  ok("featurePolicy" in ifr, "HTMLIFrameElement.featurePolicy exists");

  ifr.setAttribute("allow", "geolocation; camera 'none'");

  ok(!ifr.featurePolicy.allowsFeature("foobar"), "Random feature");
  ok(!ifr.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

  ok(!ifr.featurePolicy.allowsFeature("camera"), "Camera is not allowed");
  let allowed = ifr.featurePolicy.getAllowlistForFeature("camera");
  is(allowed.length, 0, "Camera has an empty allowlist");

  ok(ifr.featurePolicy.allowsFeature("geolocation"), "Geolocation is allowed for self");
  ok(ifr.featurePolicy.allowsFeature("geolocation", CROSS_ORIGIN), "Geolocation is allowed for self");
  ok(!ifr.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
  allowed = ifr.featurePolicy.getAllowlistForFeature("geolocation");
  is(allowed.length, 1, "Only 1 entry in allowlist for geolocation");
  is(allowed[0], CROSS_ORIGIN, "allowlist is '*'");

  ok(ifr.featurePolicy.allowsFeature("microphone"), "Microphone is enabled for self");
  ok(ifr.featurePolicy.allowsFeature("microphone", CROSS_ORIGIN), "Microphone is enabled for self");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
  ok(!ifr.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is disabled for example.com");
  allowed = ifr.featurePolicy.getAllowlistForFeature("microphone");
  is(allowed.length, 1, "Only 1 entry in allowlist for microphone");
  is(allowed[0], CROSS_ORIGIN, "allowlist is self");

  ok(!ifr.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", CROSS_ORIGIN), "Vibrate is disabled for self");
  ok(!ifr.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
  allowed = ifr.featurePolicy.getAllowlistForFeature("vr");
  is(allowed.length, 0, "No allowlist for vr");

  ok(ifr.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is allowed only for self");
  // microphone is enabled for this origin
  ok(ifr.featurePolicy.allowedFeatures().includes("microphone"), "microphone is allowed");

  next();
}

function test_cross_iframe_contentDocument_no_allow() {
  info("Checking cross iframe document.featurePolicy no allow");

  let ifr = document.createElement("iframe");
  ifr.setAttribute("src", "https://example.org/tests/dom/security/featurePolicy/test/mochitest/empty.html");
  ifr.onload = async function() {
    await SpecialPowers.spawn(ifr, [], () => {
      Assert.ok("featurePolicy" in this.content.document, "We have this.content.document.featurePolicy");

      Assert.ok(!this.content.document.featurePolicy.allowsFeature("foobar"), "Random feature");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

      Assert.ok(this.content.document.featurePolicy.allowsFeature("camera"), "Camera is allowed for self");
      Assert.ok(this.content.document.featurePolicy.allowsFeature("camera", "https://example.org"), "Camera is allowed for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("camera", "https://foo.bar"), "Camera is not allowed for a random URL");
      let allowed = this.content.document.featurePolicy.getAllowlistForFeature("camera");
      Assert.equal(allowed.length, 1, "Only 1 entry in allowlist for camera");
      Assert.equal(allowed[0], "https://example.org", "allowlist is 'self'");

      Assert.ok(!this.content.document.featurePolicy.allowsFeature("geolocation"), "Geolocation is not allowed for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("geolocation", "https://example.org"),
        "Geolocation is not allowed for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
      allowed = this.content.document.featurePolicy.getAllowlistForFeature("geolocation");
      Assert.equal(allowed.length, 0, "No allowlist for geolocation");

      Assert.ok(this.content.document.featurePolicy.allowsFeature("microphone"), "Microphone is enabled for self");
      Assert.ok(this.content.document.featurePolicy.allowsFeature("microphone", "https://example.org"), "Microphone is enabled for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is disabled for example.com");
      allowed = this.content.document.featurePolicy.getAllowlistForFeature("microphone");
      Assert.equal(allowed.length, 1, "Only 1 entry in allowlist for microphone");
      Assert.equal(allowed[0], "https://example.org", "allowlist is self");

      Assert.ok(!this.content.document.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("vr", "https://example.org"), "Vibrate is disabled for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
      allowed = this.content.document.featurePolicy.getAllowlistForFeature("vr");
      Assert.equal(allowed.length, 0, "No allowlist for vr");

      Assert.ok(this.content.document.featurePolicy.allowedFeatures().includes("camera"), "Camera is allowed");
      Assert.ok(!this.content.document.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is not allowed");
      // microphone is enabled for this origin
      Assert.ok(this.content.document.featurePolicy.allowedFeatures().includes("microphone"), "microphone is allowed");
      // vr is disabled everywhere.
      Assert.ok(!this.content.document.featurePolicy.allowedFeatures().includes("vr"), "VR is not allowed");
    });

    next();
  };
  document.body.appendChild(ifr);
}

function test_cross_iframe_contentDocument_allow() {
  info("Checking cross iframe document.featurePolicy with allow");

  let ifr = document.createElement("iframe");
  ifr.setAttribute("src", "https://example.org/tests/dom/security/featurePolicy/test/mochitest/empty.html");
  ifr.setAttribute("allow", "geolocation; camera 'none'");
  ifr.onload = async function() {
    await SpecialPowers.spawn(ifr, [], () => {
      Assert.ok("featurePolicy" in this.content.document, "We have this.content.document.featurePolicy");

      Assert.ok(!this.content.document.featurePolicy.allowsFeature("foobar"), "Random feature");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("foobar", "https://www.something.net"), "Random feature");

      Assert.ok(!this.content.document.featurePolicy.allowsFeature("camera"), "Camera is not allowed");
      let allowed = this.content.document.featurePolicy.getAllowlistForFeature("camera");
      Assert.equal(allowed.length, 0, "Camera has an empty allowlist");

      Assert.ok(this.content.document.featurePolicy.allowsFeature("geolocation"), "Geolocation is allowed for self");
      Assert.ok(this.content.document.featurePolicy.allowsFeature("geolocation", "https://example.org"), "Geolocation is allowed for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("geolocation", "https://foo.bar"), "Geolocation is not allowed for any random URL");
      allowed = this.content.document.featurePolicy.getAllowlistForFeature("geolocation");
      Assert.equal(allowed.length, 1, "Only 1 entry in allowlist for geolocation");
      Assert.equal(allowed[0], "https://example.org", "allowlist is '*'");

      Assert.ok(this.content.document.featurePolicy.allowsFeature("microphone"), "Microphone is enabled for self");
      Assert.ok(this.content.document.featurePolicy.allowsFeature("microphone", "https://example.org"), "Microphone is enabled for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("microphone", "https://foo.bar"), "Microphone is disabled for foo.bar");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("microphone", "https://example.com"), "Microphone is disabled for example.com");
      allowed = this.content.document.featurePolicy.getAllowlistForFeature("microphone");
      Assert.equal(allowed.length, 1, "Only 1 entry in allowlist for microphone");
      Assert.equal(allowed[0], "https://example.org", "allowlist is self");

      Assert.ok(!this.content.document.featurePolicy.allowsFeature("vr"), "Vibrate is disabled for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("vr", "https://example.org"), "Vibrate is disabled for self");
      Assert.ok(!this.content.document.featurePolicy.allowsFeature("vr", "https://foo.bar"), "Vibrate is disabled for foo.bar");
      allowed = this.content.document.featurePolicy.getAllowlistForFeature("vr");
      Assert.equal(allowed.length, 0, "No allowlist for vr");

      Assert.ok(this.content.document.featurePolicy.allowedFeatures().includes("geolocation"), "Geolocation is allowed only for self");
      // microphone is enabled for this origin
      Assert.ok(this.content.document.featurePolicy.allowedFeatures().includes("microphone"), "microphone is allowed");
    });

    next();
  };
  document.body.appendChild(ifr);
}


var tests = [
  test_document,
  test_iframe_without_allow,
  test_iframe_with_allow,
  test_iframe_contentDocument,
  test_cross_iframe_without_allow,
  test_cross_iframe_with_allow,
  test_cross_iframe_contentDocument_no_allow,
  test_cross_iframe_contentDocument_allow
];

function next() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

next();

</script>
</body>
</html>