summaryrefslogtreecommitdiffstats
path: root/netwerk/test/mochitests/test_1331680_iframe.html
blob: dc3397f60fcf33031471ad118f1e803ba1a194f3 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=643051
-->
<head>
  <title>Cookies set from iframe in content process</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1331680">Mozilla Bug 1331680</a>
<p id="display"></p>
<div id="content" style="display: none">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
const IFRAME_COOKIE_NAMES = ["if1", "if2_1", "if2_2"];
const ID = ["if_1", "if_2", "if_3"];

var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('file_1331680.js'));

/* Test iframe
 * 1. Create three iframes, and one of the iframe will create two cookies.
 * 2. Confirm the cookies can be proessed from observer.
 * 3. Confirm document.cookie can get cookies "if2_1" and "if2_2".
 * 4. Confirm the iframe whose source is "about:blank" can get parent's cookies.
 */
function createIframe(id, src, sandbox_flags) {
  return new Promise(resolve => {
    var ifr = document.createElement("iframe");
    ifr.id = id;
    ifr.src = src;
    ifr.sandbox = sandbox_flags;
    ifr.addEventListener("load", resolve);
    document.body.appendChild(ifr);
  });
};

function confirmCookies(id) {
  is(document.cookie, "if2_1=if2_val1; if2_2=if2_val2", "Confirm the cookies can get after iframe was deleted");
  var new_ifr = document.getElementById(id);
  is(new_ifr.contentDocument.cookie, document.cookie, "Confirm the inner document.cookie = parent document.cookie");
  document.cookie = IFRAME_COOKIE_NAMES[1] + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  document.cookie = IFRAME_COOKIE_NAMES[2] + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  is(document.cookie, "", "Removed all cookies");
  SimpleTest.finish();
}

addEventListener("message", function(event) {
  is(event.data, document.cookie, "Confirm the iframe 2 can communicate with iframe");
});

Promise.resolve()
  .then(_ => createIframe(ID[0], "file_iframe_allow_scripts.html", "allow-scripts"))
  .then(_ => createIframe(ID[1], "file_iframe_allow_same_origin.html", "allow-scripts allow-same-origin"))
  .then(_ => createIframe(ID[2], "about:blank", "allow-scripts allow-same-origin"))
  .then(_ => confirmCookies(ID[2]));

</script>

</div>
<pre id="test">
</pre>
</body>
</html>