content/canvas/test/crossorigin/test_canvas2d_crossorigin.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=685518
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 685518</title>
michael@0 8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=685518">Mozilla Bug 685518</a>
michael@0 13 <p id="display"></p>
michael@0 14 <div id="content" style="display: none">
michael@0 15
michael@0 16 </div>
michael@0 17 <pre id="test">
michael@0 18 <script type="application/javascript">
michael@0 19
michael@0 20 /** Test for Bug 685518 **/
michael@0 21
michael@0 22 SimpleTest.waitForExplicitFinish();
michael@0 23
michael@0 24 const BAD_URI_ERR = "NS_ERROR_DOM_BAD_URI_ERR";
michael@0 25 const OK = "";
michael@0 26
michael@0 27 function verifyError(actual_error, expected_error, message) {
michael@0 28 ok(actual_error == expected_error,
michael@0 29 message + ": expected " + expected_error + ", got " + actual_error);
michael@0 30 }
michael@0 31
michael@0 32 var number_of_tests_live = 0;
michael@0 33
michael@0 34 function testDone() {
michael@0 35 number_of_tests_live--;
michael@0 36
michael@0 37 if (number_of_tests_live == 0)
michael@0 38 SimpleTest.finish();
michael@0 39 }
michael@0 40
michael@0 41 function testImage(url, crossOriginAttribute, expected_error) {
michael@0 42 ++number_of_tests_live;
michael@0 43 var image;
michael@0 44 if (crossOriginAttribute == "just-crossOrigin-without-value") {
michael@0 45 var div = document.createElement('div');
michael@0 46 div.innerHTML="<img crossOrigin>";
michael@0 47 image = div.children[0];
michael@0 48 }
michael@0 49 else {
michael@0 50 image = new Image();
michael@0 51 if (crossOriginAttribute != "missing-value-default") {
michael@0 52 image.crossOrigin = crossOriginAttribute;
michael@0 53 }
michael@0 54 }
michael@0 55
michael@0 56 image.onload = function() {
michael@0 57 var c = document.createElement("canvas");
michael@0 58 c.width = this.width;
michael@0 59 c.height = this.height;
michael@0 60 var ctx = c.getContext("2d");
michael@0 61 ctx.drawImage(this, 0, 0);
michael@0 62
michael@0 63 var data;
michael@0 64 var actual_error;
michael@0 65 try {
michael@0 66 data = ctx.getImageData(0, 0, 1, 1);
michael@0 67 actual_error = OK;
michael@0 68 } catch (e) {
michael@0 69 actual_error = e.name;
michael@0 70 }
michael@0 71
michael@0 72 verifyError(actual_error, expected_error,
michael@0 73 "drawImage then get image data on " + url +
michael@0 74 " with crossOrigin=" + this.crossOrigin);
michael@0 75
michael@0 76 // Now test patterns
michael@0 77 c = document.createElement("canvas");
michael@0 78 c.width = this.width;
michael@0 79 c.height = this.height;
michael@0 80 ctx = c.getContext("2d");
michael@0 81 ctx.fillStyle = ctx.createPattern(this, "");
michael@0 82 ctx.fillRect(0, 0, c.width, c.height);
michael@0 83 try {
michael@0 84 data = ctx.getImageData(0, 0, 1, 1);
michael@0 85 actual_error = OK;
michael@0 86 } catch (e) {
michael@0 87 actual_error = e.name;
michael@0 88 }
michael@0 89
michael@0 90 verifyError(actual_error, expected_error,
michael@0 91 "createPattern+fill then get image data on " + url +
michael@0 92 " with crossOrigin=" + this.crossOrigin);
michael@0 93
michael@0 94 testDone();
michael@0 95 };
michael@0 96
michael@0 97 image.onerror = function(event) {
michael@0 98 verifyError(BAD_URI_ERR, expected_error,
michael@0 99 "image error handler for " + url +
michael@0 100 " with crossOrigin=" + this.crossOrigin);
michael@0 101
michael@0 102 testDone();
michael@0 103 }
michael@0 104
michael@0 105 image.src = url;
michael@0 106 }
michael@0 107
michael@0 108 // Now kick off the tests.
michael@0 109 const testPath = "/tests/content/canvas/test/crossorigin/"
michael@0 110
michael@0 111 // First column is image file, second column is what CORS headers the server sends
michael@0 112 const imageFiles = [
michael@0 113 [ "image.png", "none" ],
michael@0 114 [ "image-allow-star.png", "allow-all-anon" ],
michael@0 115 [ "image-allow-credentials.png", "allow-single-server-creds" ]
michael@0 116 ];
michael@0 117
michael@0 118 const hostnames = [
michael@0 119 [ "mochi.test:8888", "same-origin" ],
michael@0 120 [ "example.com", "cross-origin" ]
michael@0 121 ];
michael@0 122
michael@0 123 // First column is the value; second column is the expected resulting CORS mode
michael@0 124 const attrValues = [
michael@0 125 [ "missing-value-default", "none" ],
michael@0 126 [ "", "anonymous" ],
michael@0 127 [ "just-crossOrigin-without-value", "anonymous" ],
michael@0 128 [ "anonymous", "anonymous" ],
michael@0 129 [ "use-credentials", "use-credentials" ],
michael@0 130 [ "foobar", "anonymous" ]
michael@0 131 ];
michael@0 132
michael@0 133 for (var imgIdx = 0; imgIdx < imageFiles.length; ++imgIdx) {
michael@0 134 for (var hostnameIdx = 0; hostnameIdx < hostnames.length; ++hostnameIdx) {
michael@0 135 var hostnameData = hostnames[hostnameIdx];
michael@0 136 var url = "http://" + hostnameData[0] + testPath + imageFiles[imgIdx][0];
michael@0 137 for (var attrValIdx = 0; attrValIdx < attrValues.length; ++attrValIdx) {
michael@0 138 var attrValData = attrValues[attrValIdx];
michael@0 139 // Now compute the expected result
michael@0 140 var expected_error;
michael@0 141 if (hostnameData[1] == "same-origin") {
michael@0 142 // Same-origin; these should all Just Work
michael@0 143 expected_error = OK;
michael@0 144 } else {
michael@0 145 // Cross-origin
michael@0 146 is(hostnameData[1], "cross-origin",
michael@0 147 "what sort of host is " + hostnameData[0]);
michael@0 148 var CORSMode = attrValData[1];
michael@0 149 if (CORSMode == "none") {
michael@0 150 // Doesn't matter what headers the server sends; we're not
michael@0 151 // using CORS on our end.
michael@0 152 expected_error = "SecurityError";
michael@0 153 } else {
michael@0 154 // Check whether the server will let us talk to them
michael@0 155 var CORSHeaders = imageFiles[imgIdx][1];
michael@0 156 // We're going to look for CORS headers from the server
michael@0 157 if (CORSHeaders == "none") {
michael@0 158 // No CORS headers from server; load will fail.
michael@0 159 expected_error = BAD_URI_ERR;
michael@0 160 } else if (CORSHeaders == "allow-all-anon") {
michael@0 161 // Server only allows anonymous requests
michael@0 162 if (CORSMode == "anonymous") {
michael@0 163 expected_error = OK;
michael@0 164 } else {
michael@0 165 is(CORSMode, "use-credentials",
michael@0 166 "What other CORS modes are there?");
michael@0 167 // A load with credentials against a server that only
michael@0 168 // allows anonymous loads will fail.
michael@0 169 expected_error = BAD_URI_ERR;
michael@0 170 }
michael@0 171 } else {
michael@0 172 is(CORSHeaders, "allow-single-server-creds",
michael@0 173 "What other CORS headers could there be?");
michael@0 174 // Our server should allow both anonymous and non-anonymous requests
michael@0 175 expected_error = OK;
michael@0 176 }
michael@0 177 }
michael@0 178 }
michael@0 179 testImage(url, attrValData[0], expected_error);
michael@0 180 }
michael@0 181 }
michael@0 182 }
michael@0 183 </script>
michael@0 184 </pre>
michael@0 185 </body>
michael@0 186 </html>

mercurial