summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/test_link_rel_preload.html
blob: 18f236d56a52f35e5065bd89ce9ce0eb351cb69a (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
<!doctype html>
<html>
<head>
  <title>Bug 1599791 - Test link rel=preload</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<iframe id=testframe></iframe>
<script class="testbody" type="text/javascript">

// Please note that 'fakeServer' does not exist because the test relies
// on "csp-on-violate-policy" , and "specialpowers-http-notify-request"
// which fire if either the request is blocked or fires. The test does
// not rely on the result of the load.

let TOTAL_TESTS = 3; // script, style, image
let seenTests = 0;

function examiner() {
  SpecialPowers.addObserver(this, "csp-on-violate-policy");
  SpecialPowers.addObserver(this, "specialpowers-http-notify-request");
}
examiner.prototype  = {
  observe(subject, topic, data) {
    if (topic === "csp-on-violate-policy") {
      let asciiSpec = SpecialPowers.getPrivilegedProps(
                       SpecialPowers.do_QueryInterface(subject, "nsIURI"),
                       "asciiSpec");
      if (asciiSpec.includes("fakeServer?script") ||
          asciiSpec.includes("fakeServer?style") ||
          asciiSpec.includes("fakeServer?fetch") ||
          asciiSpec.includes("fakeServer?font") ||
          asciiSpec.includes("fakeServer?image")) {
        let type = asciiSpec.substring(asciiSpec.indexOf("?") + 1);
        ok (true, type + " should be blocked by CSP");
        checkFinished();
      }
    }

    if (topic === "specialpowers-http-notify-request") {
      if (data.includes("fakeServer?script") ||
          data.includes("fakeServer?style") ||
          data.includes("fakeServer?fetch") ||
          data.includes("fakeServer?font") ||
          data.includes("fakeServer?image")) {
        let type = data.substring(data.indexOf("?") + 1);
        ok (false, type + " should not be loaded");
        checkFinished();
      }
    }
  },
  remove() {
    SpecialPowers.removeObserver(this, "csp-on-violate-policy");
    SpecialPowers.removeObserver(this, "specialpowers-http-notify-request");
  }
}

window.examiner = new examiner();

function checkFinished() {
  seenTests++;
  if (seenTests == TOTAL_TESTS) {
    window.examiner.remove();
    SimpleTest.finish();
    return;
  }
}

SimpleTest.waitForExplicitFinish();

SpecialPowers.pushPrefEnv(
  {'set':[["network.preload", true]]},
  function() {
    document.getElementById("testframe").src = "file_link_rel_preload.html";
  });

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