Best JavaScript code snippet using wpt
fetch-canvas-tainting-tests.js
Source:fetch-canvas-tainting-tests.js
...3const TAINTED = 'TAINTED';4const LOAD_ERROR = 'LOAD_ERROR';5let frame;6// Creates a single promise_test.7function canvas_taint_test(url, cross_origin, expected_result) {8 promise_test(t => {9 return frame.contentWindow.create_test_case_promise(url, cross_origin)10 .then(result => {11 assert_equals(result, expected_result);12 });13 }, 'url "' + url + '" with crossOrigin "' + cross_origin + '" should be ' +14 expected_result);15}16// Runs all the tests. The given |params| has these properties:17// * |resource_path|: the relative path to the (image/video) resource to test.18// * |cache|: when true, the service worker bounces responses into19// Cache Storage and back out before responding with them.20function do_canvas_tainting_tests(params) {21 const host_info = get_host_info();22 let resource_path = params.resource_path;23 if (params.cache)24 resource_path += "&cache=true";25 const resource_url = host_info['HTTPS_ORIGIN'] + resource_path;26 const remote_resource_url = host_info['HTTPS_REMOTE_ORIGIN'] + resource_path;27 // Set up the service worker and the frame.28 promise_test(function(t) {29 const SCOPE = 'resources/fetch-canvas-tainting-iframe.html';30 const SCRIPT = 'resources/fetch-rewrite-worker.js';31 const host_info = get_host_info();32 // login_https() is needed because some test cases use credentials.33 return login_https(t)34 .then(function() {35 return service_worker_unregister_and_register(t, SCRIPT, SCOPE);36 })37 .then(function(registration) {38 promise_test(() => {39 if (frame)40 frame.remove();41 return registration.unregister();42 }, 'restore global state');43 return wait_for_state(t, registration.installing, 'activated');44 })45 .then(function() { return with_iframe(SCOPE); })46 .then(f => {47 frame = f;48 });49 }, 'initialize global state');50 // Reject tests. Add '&reject' so the service worker responds with a rejected promise.51 // A load error is expected.52 canvas_taint_test(resource_url + '&reject', '', LOAD_ERROR);53 canvas_taint_test(resource_url + '&reject', 'anonymous', LOAD_ERROR);54 canvas_taint_test(resource_url + '&reject', 'use-credentials', LOAD_ERROR);55 // Fallback tests. Add '&ignore' so the service worker does not respond to the fetch56 // request, and we fall back to network.57 canvas_taint_test(resource_url + '&ignore', '', NOT_TAINTED);58 canvas_taint_test(remote_resource_url + '&ignore', '', TAINTED);59 canvas_taint_test(remote_resource_url + '&ignore', 'anonymous', LOAD_ERROR);60 canvas_taint_test(61 remote_resource_url + '&ACAOrigin=' + host_info['HTTPS_ORIGIN'] +62 '&ignore',63 'anonymous',64 NOT_TAINTED);65 canvas_taint_test(remote_resource_url + '&ignore', 'use-credentials', LOAD_ERROR);66 canvas_taint_test(67 remote_resource_url + '&ACAOrigin=' + host_info['HTTPS_ORIGIN'] +68 '&ignore',69 'use-credentials',70 LOAD_ERROR);71 canvas_taint_test(72 remote_resource_url + '&ACAOrigin=' + host_info['HTTPS_ORIGIN'] +73 '&ACACredentials=true&ignore',74 'use-credentials',75 NOT_TAINTED);76 // Credential tests (with fallback). Add '&Auth' so the server requires authentication.77 // Furthermore, add '&ignore' so the service worker falls back to network.78 canvas_taint_test(resource_url + '&Auth&ignore', '', NOT_TAINTED);79 canvas_taint_test(remote_resource_url + '&Auth&ignore', '', TAINTED);80 canvas_taint_test(81 remote_resource_url + '&Auth&ignore', 'anonymous', LOAD_ERROR);82 canvas_taint_test(83 remote_resource_url + '&Auth&ignore',84 'use-credentials',85 LOAD_ERROR);86 canvas_taint_test(87 remote_resource_url + '&Auth&ACAOrigin=' + host_info['HTTPS_ORIGIN'] +88 '&ignore',89 'use-credentials',90 LOAD_ERROR);91 canvas_taint_test(92 remote_resource_url + '&Auth&ACAOrigin=' + host_info['HTTPS_ORIGIN'] +93 '&ACACredentials=true&ignore',94 'use-credentials',95 NOT_TAINTED);96 // In the following tests, the service worker provides a response.97 // Add '&url' so the service worker responds with fetch(url).98 // Add '&mode' to configure the fetch request options.99 // Basic response tests. Set &url to the original url.100 canvas_taint_test(101 resource_url + '&mode=same-origin&url=' + encodeURIComponent(resource_url),102 '',103 NOT_TAINTED);104 canvas_taint_test(105 resource_url + '&mode=same-origin&url=' + encodeURIComponent(resource_url),106 'anonymous',107 NOT_TAINTED);108 canvas_taint_test(109 resource_url + '&mode=same-origin&url=' + encodeURIComponent(resource_url),110 'use-credentials',111 NOT_TAINTED);112 canvas_taint_test(113 remote_resource_url + '&mode=same-origin&url=' +114 encodeURIComponent(resource_url),115 '',116 NOT_TAINTED);117 canvas_taint_test(118 remote_resource_url + '&mode=same-origin&url=' +119 encodeURIComponent(resource_url),120 'anonymous',121 NOT_TAINTED);122 canvas_taint_test(123 remote_resource_url + '&mode=same-origin&url=' +124 encodeURIComponent(resource_url),125 'use-credentials',126 NOT_TAINTED);127 // Opaque response tests. Set &url to the cross-origin URL, and &mode to128 // 'no-cors' so we expect an opaque response.129 canvas_taint_test(130 resource_url +131 '&mode=no-cors&url=' + encodeURIComponent(remote_resource_url),132 '',133 TAINTED);134 canvas_taint_test(135 resource_url +136 '&mode=no-cors&url=' + encodeURIComponent(remote_resource_url),137 'anonymous',138 LOAD_ERROR);139 canvas_taint_test(140 resource_url +141 '&mode=no-cors&url=' + encodeURIComponent(remote_resource_url),142 'use-credentials',143 LOAD_ERROR);144 canvas_taint_test(145 remote_resource_url +146 '&mode=no-cors&url=' + encodeURIComponent(remote_resource_url),147 '',148 TAINTED);149 canvas_taint_test(150 remote_resource_url +151 '&mode=no-cors&url=' + encodeURIComponent(remote_resource_url),152 'anonymous',153 LOAD_ERROR);154 canvas_taint_test(155 remote_resource_url +156 '&mode=no-cors&url=' + encodeURIComponent(remote_resource_url),157 'use-credentials',158 LOAD_ERROR);159 // CORS response tests. Set &url to the cross-origin URL, and &mode160 // to 'cors' to attempt a CORS request.161 canvas_taint_test(162 resource_url + '&mode=cors&url=' +163 encodeURIComponent(remote_resource_url +164 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),165 '',166 LOAD_ERROR); // We expect LOAD_ERROR since the server doesn't respond167 // with an Access-Control-Allow-Credentials header.168 canvas_taint_test(169 resource_url + '&mode=cors&credentials=same-origin&url=' +170 encodeURIComponent(remote_resource_url +171 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),172 '',173 NOT_TAINTED);174 canvas_taint_test(175 resource_url + '&mode=cors&url=' +176 encodeURIComponent(remote_resource_url +177 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),178 'anonymous',179 NOT_TAINTED);180 canvas_taint_test(181 resource_url + '&mode=cors&url=' +182 encodeURIComponent(remote_resource_url +183 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),184 'use-credentials',185 LOAD_ERROR); // We expect LOAD_ERROR since the server doesn't respond186 // with an Access-Control-Allow-Credentials header.187 canvas_taint_test(188 resource_url + '&mode=cors&url=' +189 encodeURIComponent(190 remote_resource_url +191 '&ACACredentials=true&ACAOrigin=' + host_info['HTTPS_ORIGIN']),192 'use-credentials',193 NOT_TAINTED);194 canvas_taint_test(195 remote_resource_url + '&mode=cors&url=' +196 encodeURIComponent(remote_resource_url +197 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),198 '',199 LOAD_ERROR); // We expect LOAD_ERROR since the server doesn't respond200 // with an Access-Control-Allow-Credentials header.201 canvas_taint_test(202 remote_resource_url + '&mode=cors&credentials=same-origin&url=' +203 encodeURIComponent(remote_resource_url +204 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),205 '',206 NOT_TAINTED);207 canvas_taint_test(208 remote_resource_url + '&mode=cors&url=' +209 encodeURIComponent(remote_resource_url +210 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),211 'anonymous',212 NOT_TAINTED);213 canvas_taint_test(214 remote_resource_url + '&mode=cors&url=' +215 encodeURIComponent(remote_resource_url +216 '&ACAOrigin=' + host_info['HTTPS_ORIGIN']),217 'use-credentials',218 LOAD_ERROR); // We expect LOAD_ERROR since the server doesn't respond219 // with an Access-Control-Allow-Credentials header.220 canvas_taint_test(221 remote_resource_url + '&mode=cors&url=' +222 encodeURIComponent(223 remote_resource_url +224 '&ACACredentials=true&ACAOrigin=' + host_info['HTTPS_ORIGIN']),225 'use-credentials',226 NOT_TAINTED);...
Using AI Code Generation
1var canvas = document.createElement("canvas");2var ctx = canvas.getContext("2d");3ctx.fillStyle = "rgb(200,0,0)";4ctx.fillRect (10, 10, 55, 50);5ctx.fillStyle = "rgba(0, 0, 200, 0.5)";6ctx.fillRect (30, 30, 55, 50);7var data = canvas.toDataURL("image/png");8var canvas_taint_test = async_test("Canvas is not tainted");9canvas_taint_test.step(function() {10 assert_false(canvas.isTainted, "Canvas is not tainted");11 canvas_taint_test.done();12});
Using AI Code Generation
1function runTest() {2 var canvas = document.getElementById('canvas');3 var ctx = canvas.getContext('2d');4 ctx.fillStyle = '#000000';5 ctx.fillRect(0, 0, canvas.width, canvas.height);6 canvas.toDataURL();7 var t = async_test('Canvas taint test');8 t.step(function() {9 var result = canvas_taint_test(canvas, function() {10 t.done();11 });12 });13}14<body onload="runTest()">
Using AI Code Generation
1var canvas = document.createElement('canvas');2var ctx = canvas.getContext('2d');3ctx.rect(0, 0, 10, 10);4ctx.fillStyle = 'red';5ctx.fill();6ctx.save();7ctx.fillStyle = 'blue';8ctx.fillRect(0, 0, 10, 10);9ctx.restore();10ctx.fillRect(0, 0, 10, 10);11var canvas_taint_test = window.canvas_taint_test;12canvas_taint_test(canvas, function (isTainted) {13 if (isTainted) {14 console.log('Canvas is tainted');15 } else {16 console.log('Canvas is not tainted');17 }18});19var canvas = document.createElement('canvas');20var ctx = canvas.getContext('2d');21ctx.rect(0, 0, 10, 10);22ctx.fillStyle = 'red';23ctx.fill();24ctx.save();25ctx.fillStyle = 'blue';26ctx.fillRect(0, 0, 10, 10);27ctx.restore();28ctx.fillRect(0, 0, 10, 10);29var canvas_taint_test = window.canvas_taint_test;30canvas_taint_test(canvas, function (isTainted) {31 if (isTainted) {32 console.log('Canvas is tainted');33 } else {34 console.log('Canvas is not tainted');35 }36});37var canvas = document.createElement('canvas');38var ctx = canvas.getContext('2d');39ctx.rect(0, 0, 10, 10);40ctx.fillStyle = 'red';41ctx.fill();42ctx.save();43ctx.fillStyle = 'blue';44ctx.fillRect(0, 0, 10, 10);45ctx.restore();46ctx.fillRect(0, 0, 10, 10);47var canvas_taint_test = window.canvas_taint_test;48canvas_taint_test(canvas, function (isTainted) {49 if (isTainted) {50 console.log('Canvas is tainted');51 } else {52 console.log('Canvas is not tainted');53 }54});55var canvas = document.createElement('canvas');56var ctx = canvas.getContext('2d');57ctx.rect(0, 0, 10
Using AI Code Generation
1function test() {2 var canvas = document.getElementById("myCanvas");3 var ctx = canvas.getContext("2d");4 ctx.fillStyle = "red";5 ctx.fillRect(0, 0, 150, 75);6 return canvas.toDataURL();7}8function test2() {9 var canvas = document.getElementById("myCanvas");10 var ctx = canvas.getContext("2d");11 ctx.fillStyle = "blue";12 ctx.fillRect(0, 0, 150, 75);13 return canvas.toDataURL();14}15function test3() {16 var canvas = document.getElementById("myCanvas");17 var ctx = canvas.getContext("2d");18 ctx.fillStyle = "green";19 ctx.fillRect(0, 0, 150, 75);20 return canvas.toDataURL();21}22function test4() {23 var canvas = document.getElementById("myCanvas");24 var ctx = canvas.getContext("2d");25 ctx.fillStyle = "yellow";26 ctx.fillRect(0, 0, 150, 75);27 return canvas.toDataURL();28}29function test5() {30 var canvas = document.getElementById("myCanvas");31 var ctx = canvas.getContext("2d");32 ctx.fillStyle = "purple";33 ctx.fillRect(0, 0, 150, 75);34 return canvas.toDataURL();35}36function test6() {37 var canvas = document.getElementById("myCanvas");38 var ctx = canvas.getContext("2d");39 ctx.fillStyle = "orange";40 ctx.fillRect(0, 0, 150, 75);41 return canvas.toDataURL();42}43function test7() {44 var canvas = document.getElementById("myCanvas");45 var ctx = canvas.getContext("2d");46 ctx.fillStyle = "black";47 ctx.fillRect(0, 0, 150, 75);48 return canvas.toDataURL();49}
Using AI Code Generation
1var wpt = require('wpt-driver');2 console.log(res);3});4var wpt = require('wpt-driver');5 console.log(res);6});7var wpt = require('wpt-driver');8 console.log(res);9});10var wpt = require('wpt-driver');11 console.log(res);12});13var wpt = require('wpt-driver');14 console.log(res);15});16var wpt = require('wpt-driver');17 console.log(res);18});19var wpt = require('wpt-driver');20 console.log(res);21});
Using AI Code Generation
1window.onload = function() {2 var canvas = document.createElement("canvas");3 var ctx = canvas.getContext("2d");4 var img = new Image();5 img.onload = function() {6 ctx.drawImage(img, 0, 0);7 var data = canvas.toDataURL();8 var tainted = false;9 try {10 var pixel = ctx.getImageData(0, 0, 1, 1);11 } catch(e) {12 tainted = true;13 }14 if (tainted) {15 wptdriver.canvas_taint_test(true);16 } else {17 wptdriver.canvas_taint_test(false);18 }19 };20 img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmTawAAAABJRU5ErkJggg==";21}22window.onload = function() {23 var canvas = document.createElement("canvas");24 var ctx = canvas.getContext("2d");25 var img = new Image();26 img.onload = function() {27 ctx.drawImage(img, 0, 0);28 var data = canvas.toDataURL();29 var tainted = false;30 try {31 var pixel = ctx.getImageData(0, 0, 1, 1);32 } catch(e) {33 tainted = true;34 }35 if (tainted) {36 wptdriver.canvas_taint_test(true);37 } else {38 wptdriver.canvas_taint_test(false);39 }40 };41 img.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmTawAAAABJRU5ErkJggg==";42}
Using AI Code Generation
1function canvas_taint_test(canvas, url) {2 var context = canvas.getContext("2d");3 var img = document.createElement("img");4 img.src = url;5 context.drawImage(img, 0, 0);6 test(function() {7 assert_false(context.isPointInPath(0, 0), "Point in path test");8 }, "Canvas taint test for " + url);9}10function canvas_taint_test(canvas, url) {11 var context = canvas.getContext("2d");12 var img = document.createElement("img");13 img.src = url;14 context.drawImage(img, 0, 0);15 test(function() {16 assert_false(context.isPointInPath(0, 0), "Point in path test");17 }, "Canvas taint test for " + url);18}19function canvas_taint_test(canvas, url) {20 var context = canvas.getContext("2d");21 var img = document.createElement("img");22 img.src = url;23 context.drawImage(img, 0, 0);24 test(function() {25 assert_false(context.isPointInPath(0, 0), "Point in path test");26 }, "Canvas taint test for " + url);27}28function canvas_taint_test(canvas, url) {29 var context = canvas.getContext("2d");30 var img = document.createElement("img");31 img.src = url;32 context.drawImage(img, 0, 0);33 test(function() {34 assert_false(context.isPointInPath(0, 0), "Point in path test");35 }, "Canvas taint test for " + url);36}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!