summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/trusted-types/empty-default-policy.html
blob: d31b48ecd5a86670b7c6ea3c4d993cd3f11946df (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
<!DOCTYPE html>
<html>
<head>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
// Ensure that only the right events trigger violation reports.
// The Promise will resolve, when an event including the string "done" is
// received. The last line of this test file will cause this trigger.
promise_test(t => {
  let count = 0;
  return new Promise((resolve, reject) => {
    document.addEventListener("securitypolicyviolation", e => {
      e.stopPropagation();
      // We count the violation reports. We expect one each for "abc" test cases, and one
      // for the "done" line at the end, which signals the end of the test run.
      if (e.sample.includes("done")) {
        resolve(count);
      } else if (e.sample.includes("abc")) {
        count++;
      } else {
        reject();
      }
    });
  }).then(counter => {
    assert_equals(counter, testCases.length, "event count");
  });
}, "Count SecurityPolicyViolation events.");

const testCases = [
  [ "script", "src" ],
  [ "div", "innerHTML" ],
  [ "script", "text" ],
];

trustedTypes.createPolicy("default", {});

testCases.forEach(c => {
  const name = `${c[0]}.${c[1]} `;
  test(t => {
    const element = document.createElement(c[0]);
    assert_throws_js(TypeError, _ => element[c[1]] = "abc");
    assert_equals(element[c[1]], "");
  }, name + "default");
});

// Trigger the exit condition in the "Count" promise test above.
try { document.createElement("script").text = "done"; } catch (e) {}
</script>
</body>