1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8">
6 <title>WebGL Origin Restrictions Conformance Tests
</title>
8 function create3DContext(canvas
, attributes
)
11 canvas
= document
.createElement("canvas");
14 context
= canvas
.getContext("webgl", attributes
);
18 context
= canvas
.getContext("webkit-3d", attributes
);
23 context
= canvas
.getContext("moz-webgl", attributes
);
27 throw "Unable to fetch WebGL rendering context for Canvas";
32 function description(msg
)
34 // For MSIE 6 compatibility
35 var span
= document
.createElement("span");
36 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>';
37 var description
= document
.getElementById("description");
38 if (description
.firstChild
)
39 description
.replaceChild(span
, description
.firstChild
);
41 description
.appendChild(span
);
46 var span
= document
.createElement("span");
47 document
.getElementById("console").appendChild(span
); // insert it first so XHTML knows the namespace
48 span
.innerHTML
= msg
+ '<br />';
51 function escapeHTML(text
)
53 return text
.replace(/&/g, "&").replace(/</g, "<").replace(/\0/g
, "\\0");
56 function testPassed(msg
)
58 debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg
) + '</span>');
61 function testFailed(msg
)
63 debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg
) + '</span>');
66 function assertMsg(assertion
, msg
) {
74 // Checks if function throws an exception.
75 function causedException(func
) {
76 var hadException
= false;
85 var testVideo
= false;
88 var video
= document
.getElementById("video");
90 var base
= "http://localhost:8000/resources/";
92 ["video/mp4", base
+ "test.mp4"],
93 ["video/ogg", base
+ "test.ogv"],
96 for (var i
= 0; i
< videos
.length
; ++i
) {
97 if (video
.canPlayType(videos
[i
][0])) {
98 videoFile
= videos
[i
][1];
102 assertMsg(videoFile
, "Playable video format found");
105 if (window
.testRunner
) {
106 testRunner
.overridePreference("WebKitWebGLEnabled", "1");
107 testRunner
.dumpAsText();
108 testRunner
.waitUntilDone();
110 video
.src
= videoFile
;
111 video
.addEventListener("playing", runTests
);
115 // Still run the other tests, even if the video failed.
120 function runTests() {
121 description("This test ensures WebGL implementations follow proper same-origin restrictions.");
122 var img
= document
.getElementById("img");
123 assertMsg(img
.width
> 0 && img
.height
> 0, "img was loaded");
125 function makeTexImage2D(gl
, src
) {
127 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, src
);
131 function makeTexSubImage2D(gl
, src
) {
133 gl
.texSubImage2D(gl
.TEXTURE_2D
, 0, 0, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, src
);
137 function makeReadPixels(gl
) {
139 var buf
= new Uint8Array(4);
140 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, buf
);
144 function makeToDataURL(canvas
) {
146 var data
= canvas
.toDataURL();
150 var canvas1
= document
.getElementById("canvas1");
151 var gl
= create3DContext(canvas1
);
154 debug("check that an attempt to upload an image from another origin throws an exception.");
155 var tex
= gl
.createTexture();
156 gl
.bindTexture(gl
.TEXTURE_2D
, tex
);
157 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, 256, 256, 0, gl
.RGBA
, gl
.UNSIGNED_BYTE
, null);
158 assertMsg(causedException(makeTexImage2D(gl
, img
)),
159 "texImage2D with cross-origin image should throw exception.");
160 assertMsg(causedException(makeTexSubImage2D(gl
, img
)),
161 "texSubImage2D with cross-origin image should throw exception.");
163 debug("check that readPixels and toDataURL continue to work against this canvas.");
164 assertMsg(!causedException(makeReadPixels(gl
)),
165 "readPixels should never throw exception -- not possible to dirty origin of WebGL canvas.");
166 assertMsg(!causedException(makeToDataURL(canvas1
)),
167 "should not throw exception by toDataURL for WebGL canvas, which should stay origin clean.");
169 debug("check that an attempt to upload a tainted canvas throws an exception.");
170 var canvas2
= document
.getElementById("canvas2");
171 var ctx2d
= canvas2
.getContext("2d");
172 ctx2d
.drawImage(img
, 0, 0);
173 assertMsg(causedException(makeToDataURL(canvas2
)),
174 "should throw exception by toDataURL for NON origin clean canvas.");
175 assertMsg(causedException(makeTexImage2D(gl
, canvas2
)),
176 "texImage2D with NON origin clean canvas should throw exception.");
177 assertMsg(causedException(makeTexSubImage2D(gl
, canvas2
)),
178 "texSubImage2D with NON origin clean canvas should throw exception.");
180 debug("check that readPixels and toDataURL continue to work against this canvas.");
181 assertMsg(!causedException(makeReadPixels(gl
)),
182 "readPixels should never throw exception -- not possible to dirty origin of WebGL canvas.");
183 assertMsg(!causedException(makeToDataURL(canvas1
)),
184 "should not throw exception by toDataURL for WebGL canvas, which should stay origin clean.");
187 debug("check that an attempt to upload a video from another origin throws an exception.");
188 var video
= document
.getElementById("video");
189 assertMsg(causedException(makeTexImage2D(gl
, video
)),
190 "texImage2D with cross-origin video should throw exception.");
191 assertMsg(causedException(makeTexSubImage2D(gl
, video
)),
192 "texSubImage2D with cross-origin video should throw exception.");
194 debug("check that readPixels and toDataURL continue to work against this canvas.");
195 assertMsg(!causedException(makeReadPixels(gl
)),
196 "readPixels should never throw exception -- not possible to dirty origin of WebGL canvas.");
197 assertMsg(!causedException(makeToDataURL(canvas1
)),
198 "should not throw exception by toDataURL for WebGL canvas, which should stay origin clean.");
201 debug('<br /><span class="pass">TEST COMPLETE</span>');
202 if (window
.testRunner
)
203 testRunner
.waitUntilDone();
204 if (window
.testRunner
) {
205 testRunner
.notifyDone();
210 <body onload=
"init()">
211 <div id=
"description"></div>
212 <div id=
"console"></div>
213 <canvas id=
"canvas1"></canvas>
214 <canvas id=
"canvas2"></canvas>
215 <img id=
"img" src=
"http://localhost:8000/local/resources/abe.png" style=
"display:none;">
216 <video id=
"video" style=
"display:none;"/>