• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var SRIScriptTest = function(pass, name, src, integrityValue, crossoriginValue, nonce) {
2    this.pass = pass;
3    this.name = "Script: " + name;
4    this.src = src;
5    this.integrityValue = integrityValue;
6    this.crossoriginValue = crossoriginValue;
7    this.nonce = nonce;
8}
9
10SRIScriptTest.prototype.execute = function() {
11    var test = async_test(this.name);
12    var e = document.createElement("script");
13    e.src = this.src;
14    e.setAttribute("integrity", this.integrityValue);
15    if(this.crossoriginValue) {
16        e.setAttribute("crossorigin", this.crossoriginValue);
17    }
18    if(this.nonce) {
19      e.setAttribute("nonce", this.nonce);
20    }
21    if(this.pass) {
22        e.addEventListener("load", function() {test.done()});
23        e.addEventListener("error", function() {
24            test.step(function(){ assert_unreached("Good load fired error handler.") })
25        });
26    } else {
27       e.addEventListener("load", function() {
28            test.step(function() { assert_unreached("Bad load succeeded.") })
29        });
30       e.addEventListener("error", function() {test.done()});
31    }
32    document.body.appendChild(e);
33};
34
35// <link> tests
36// Style tests must be done synchronously because they rely on the presence
37// and absence of global style, which can affect later tests. Thus, instead
38// of executing them one at a time, the style tests are implemented as a
39// queue that builds up a list of tests, and then executes them one at a
40// time.
41var SRIStyleTest = function(queue, pass, name, attrs, customCallback, altPassValue) {
42    this.pass = pass;
43    this.name = "Style: " + name;
44    this.customCallback = customCallback || function () {};
45    this.attrs = attrs || {};
46    this.passValue = altPassValue || "rgb(255, 255, 0)";
47
48    this.test = async_test(this.name);
49
50    this.queue = queue;
51    this.queue.push(this);
52}
53
54SRIStyleTest.prototype.execute = function() {
55  var that = this;
56    var container = document.getElementById("container");
57    while (container.hasChildNodes()) {
58      container.removeChild(container.firstChild);
59    }
60
61    var test = this.test;
62
63    var div = document.createElement("div");
64    div.className = "testdiv";
65    var e = document.createElement("link");
66    this.attrs.rel = this.attrs.rel || "stylesheet";
67    for (var key in this.attrs) {
68        if (this.attrs.hasOwnProperty(key)) {
69            e.setAttribute(key, this.attrs[key]);
70        }
71    }
72
73    if(this.pass) {
74        e.addEventListener("load", function() {
75            test.step(function() {
76                var background = window.getComputedStyle(div, null).getPropertyValue("background-color");
77                assert_equals(background, that.passValue);
78                test.done();
79            });
80        });
81        e.addEventListener("error", function() {
82            test.step(function(){ assert_unreached("Good load fired error handler.") })
83        });
84    } else {
85        e.addEventListener("load", function() {
86             test.step(function() { assert_unreached("Bad load succeeded.") })
87         });
88        e.addEventListener("error", function() {
89            test.step(function() {
90                var background = window.getComputedStyle(div, null).getPropertyValue("background-color");
91                assert_not_equals(background, that.passValue);
92                test.done();
93            });
94        });
95    }
96    container.appendChild(div);
97    container.appendChild(e);
98    this.customCallback(e, container);
99};
100