summaryrefslogtreecommitdiffstats
path: root/LayoutTests/http/tests/canvas/webgl/origin-clean-conformance.html
blob: e983616bd4da9dddb93602eed763ac706a2750ca (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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebGL Origin Restrictions Conformance Tests</title>
<script>
function create3DContext(canvas, attributes)
{
    if (!canvas)
        canvas = document.createElement("canvas");
    var context = null;
    try {
        context = canvas.getContext("experimental-webgl", attributes);
    } catch(e) {}
    if (!context) {
        try {
            context = canvas.getContext("webkit-3d", attributes);
        } catch(e) {}
    }
    if (!context) {
        try {
            context = canvas.getContext("moz-webgl", attributes);
        } catch(e) {}
    }
    if (!context) {
        throw "Unable to fetch WebGL rendering context for Canvas";
    }
    return context;
}

function description(msg)
{
    // For MSIE 6 compatibility
    var span = document.createElement("span");
    span.innerHTML = '<p>' + msg + '</p><p>On success, you will see a series of "<span class="pass">PASS</span>" messages, followed by "<span class="pass">TEST COMPLETE</span>".</p>';
    var description = document.getElementById("description");
    if (description.firstChild)
        description.replaceChild(span, description.firstChild);
    else
        description.appendChild(span);
}

function debug(msg)
{
    var span = document.createElement("span");
    document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace
    span.innerHTML = msg + '<br />';
}

function escapeHTML(text)
{
    return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/\0/g, "\\0");
}

function testPassed(msg)
{
    debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>');
}

function testFailed(msg)
{
    debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>');
}

function assertMsg(assertion, msg) {
    if (assertion) {
        testPassed(msg);
    } else {
        testFailed(msg);
    }
}

// Checks if function throws an exception.
function causedException(func) {
  var hadException = false;
  try {
    func();
  } catch(e) {
    hadException = true;
  }
  return hadException;
}

var testVideo = false;

function init() {
  var video = document.getElementById("video");

  var base = "http://localhost:8000/resources/";
  var videos = [
    ["video/mp4", base + "test.mp4"],
    ["video/ogg", base + "test.ogv"],
  ];
  var videoFile = null;
  for (var i = 0; i < videos.length; ++i) {
    if (video.canPlayType(videos[i][0])) {
      videoFile = videos[i][1];
      break;
    }
  }
  assertMsg(videoFile, "Playable video format found");

  if (videoFile) {
    if (window.layoutTestController) {
      layoutTestController.overridePreference("WebKitWebGLEnabled", "1");
      layoutTestController.dumpAsText();
      layoutTestController.waitUntilDone();
    }
    video.src = videoFile;
    video.addEventListener("playing", runTests);
    video.play();
    testVideo = true;
  } else {
    // Still run the other tests, even if the video failed.
    runTests();
  }
}

function runTests() {
  description("This test ensures WebGL implementations follow proper same-origin restrictions.");
  var img = document.getElementById("img");
  assertMsg(img.width > 0 && img.height > 0, "img was loaded");

  function makeTexImage2D(gl, src) {
    return function() {
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, src);
    };
  }

  function makeTexSubImage2D(gl, src) {
    return function() {
      gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, src);
    };
  }

  function makeReadPixels(gl) {
    return function() {
      var buf = new Uint8Array(4);
      gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    };
  }

  function makeToDataURL(canvas) {
    return function() {
      var data = canvas.toDataURL();
    }
  }

  var canvas1 = document.getElementById("canvas1");
  var gl = create3DContext(canvas1);

  debug("");
  debug("check that an attempt to upload an image from another origin throws an exception.");
  var tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  assertMsg(causedException(makeTexImage2D(gl, img)),
            "texImage2D with cross-origin image should throw exception.");
  assertMsg(causedException(makeTexSubImage2D(gl, img)),
            "texSubImage2D with cross-origin image should throw exception.");

  debug("check that readPixels and toDataURL continue to work against this canvas.");
  assertMsg(!causedException(makeReadPixels(gl)),
            "readPixels should never throw exception -- not possible to dirty origin of WebGL canvas.");
  assertMsg(!causedException(makeToDataURL(canvas1)),
            "should not throw exception by toDataURL for WebGL canvas, which should stay origin clean.");

  debug("check that an attempt to upload a tainted canvas throws an exception.");
  var canvas2 = document.getElementById("canvas2");
  var ctx2d = canvas2.getContext("2d");
  ctx2d.drawImage(img, 0, 0);
  assertMsg(causedException(makeToDataURL(canvas2)),
            "should throw exception by toDataURL for NON origin clean canvas.");
  assertMsg(causedException(makeTexImage2D(gl, canvas2)),
            "texImage2D with NON origin clean canvas should throw exception.");
  assertMsg(causedException(makeTexSubImage2D(gl, canvas2)),
            "texSubImage2D with NON origin clean canvas should throw exception.");

  debug("check that readPixels and toDataURL continue to work against this canvas.");
  assertMsg(!causedException(makeReadPixels(gl)),
            "readPixels should never throw exception -- not possible to dirty origin of WebGL canvas.");
  assertMsg(!causedException(makeToDataURL(canvas1)),
            "should not throw exception by toDataURL for WebGL canvas, which should stay origin clean.");

  if (testVideo) {
    debug("check that an attempt to upload a video from another origin throws an exception.");
    var video = document.getElementById("video");
    assertMsg(causedException(makeTexImage2D(gl, video)),
              "texImage2D with cross-origin video should throw exception.");
    assertMsg(causedException(makeTexSubImage2D(gl, video)),
              "texSubImage2D with cross-origin video should throw exception.");

    debug("check that readPixels and toDataURL continue to work against this canvas.");
    assertMsg(!causedException(makeReadPixels(gl)),
              "readPixels should never throw exception -- not possible to dirty origin of WebGL canvas.");
    assertMsg(!causedException(makeToDataURL(canvas1)),
              "should not throw exception by toDataURL for WebGL canvas, which should stay origin clean.");
  }

  debug('<br /><span class="pass">TEST COMPLETE</span>');
  if (window.layoutTestController)
    layoutTestController.waitUntilDone();
  if (window.layoutTestController) {
    layoutTestController.notifyDone();
  }
}
</script>
</head>
<body onload="init()">
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas1"></canvas>
<canvas id="canvas2"></canvas>
<img id="img" src="http://localhost:8000/local/resources/abe.png" style="display:none;">
<video id="video" style="display:none;"/>
</body>
</html>