4 https://bugzilla.mozilla.org/show_bug.cgi?id=685518
7 <title>Test for Bug
685518</title>
8 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css"/>
12 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=685518">Mozilla Bug
685518</a>
14 <div id=
"content" style=
"display: none">
18 <script type=
"application/javascript">
20 /** Test for Bug
685518 **/
22 SimpleTest.waitForExplicitFinish();
24 const BAD_URI_ERR =
"NS_ERROR_DOM_BAD_URI_ERR";
27 function verifyError(actual_error, expected_error, message) {
28 ok(actual_error == expected_error,
29 message +
": expected " + expected_error +
", got " + actual_error);
32 var number_of_tests_live =
0;
35 number_of_tests_live--;
37 if (number_of_tests_live ==
0)
41 function testImage(url, crossOriginAttribute, expected_error) {
42 ++number_of_tests_live;
44 if (crossOriginAttribute ==
"just-crossOrigin-without-value") {
45 var div = document.createElement('div');
46 div.
innerHTML=
"<img crossOrigin>";
47 image = div.children[
0];
51 if (crossOriginAttribute !=
"missing-value-default") {
52 image.crossOrigin = crossOriginAttribute;
56 image.onload = function() {
57 var c = document.createElement(
"canvas");
59 c.height = this.height;
60 var ctx = c.getContext(
"2d");
61 ctx.drawImage(this,
0,
0);
66 data = ctx.getImageData(
0,
0,
1,
1);
69 actual_error = e.name;
72 verifyError(actual_error, expected_error,
73 "drawImage then get image data on " + url +
74 " with crossOrigin=" + this.crossOrigin);
80 actual_error = e.name;
83 verifyError(actual_error, expected_error,
84 "drawImage then capture stream on " + url +
85 " with crossOrigin=" + this.crossOrigin);
88 c = document.createElement(
"canvas");
90 c.height = this.height;
91 ctx = c.getContext(
"2d");
92 ctx.fillStyle = ctx.createPattern(this,
"");
93 ctx.fillRect(
0,
0, c.width, c.height);
95 data = ctx.getImageData(
0,
0,
1,
1);
98 actual_error = e.name;
101 verifyError(actual_error, expected_error,
102 "createPattern+fill then get image data on " + url +
103 " with crossOrigin=" + this.crossOrigin);
109 actual_error = e.name;
112 verifyError(actual_error, expected_error,
113 "createPattern+fill then capture stream on " + url +
114 " with crossOrigin=" + this.crossOrigin);
119 image.onerror = function(event) {
120 verifyError(BAD_URI_ERR, expected_error,
121 "image error handler for " + url +
122 " with crossOrigin=" + this.crossOrigin);
130 // Now kick off the tests.
131 const testPath =
"/tests/dom/canvas/test/crossorigin/"
133 // First column is image file, second column is what CORS headers the server sends
135 [
"image.png",
"none" ],
136 [
"image-allow-star.png",
"allow-all-anon" ],
137 [
"image-allow-credentials.png",
"allow-single-server-creds" ]
141 [
"mochi.test:8888",
"same-origin" ],
142 [
"example.com",
"cross-origin" ]
145 // First column is the value; second column is the expected resulting CORS mode
147 [
"missing-value-default",
"none" ],
149 [
"just-crossOrigin-without-value",
"anonymous" ],
150 [
"anonymous",
"anonymous" ],
151 [
"use-credentials",
"use-credentials" ],
152 [
"foobar",
"anonymous" ]
155 function beginTest() {
156 for (var imgIdx =
0; imgIdx < imageFiles.length; ++imgIdx) {
157 for (var hostnameIdx =
0; hostnameIdx < hostnames.length; ++hostnameIdx) {
158 var hostnameData = hostnames[hostnameIdx];
159 var url =
"http://" + hostnameData[
0] + testPath + imageFiles[imgIdx][
0];
160 for (var attrValIdx =
0; attrValIdx < attrValues.length; ++attrValIdx) {
161 var attrValData = attrValues[attrValIdx];
162 // Now compute the expected result
164 if (hostnameData[
1] ==
"same-origin") {
165 // Same-origin; these should all Just Work
169 is(hostnameData[
1],
"cross-origin",
170 "what sort of host is " + hostnameData[
0]);
171 var CORSMode = attrValData[
1];
172 if (CORSMode ==
"none") {
173 // Doesn't matter what headers the server sends; we're not
174 // using CORS on our end.
175 expected_error =
"SecurityError";
177 // Check whether the server will let us talk to them
178 var CORSHeaders = imageFiles[imgIdx][
1];
179 // We're going to look for CORS headers from the server
180 if (CORSHeaders ==
"none") {
181 // No CORS headers from server; load will fail.
182 expected_error = BAD_URI_ERR;
183 } else if (CORSHeaders ==
"allow-all-anon") {
184 // Server only allows anonymous requests
185 if (CORSMode ==
"anonymous") {
188 is(CORSMode,
"use-credentials",
189 "What other CORS modes are there?");
190 // A load with credentials against a server that only
191 // allows anonymous loads will fail.
192 expected_error = BAD_URI_ERR;
195 is(CORSHeaders,
"allow-single-server-creds",
196 "What other CORS headers could there be?");
197 // Our server should allow both anonymous and non-anonymous requests
202 testImage(url, attrValData[
0], expected_error);