Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: chrome/content/tests/policy.js

Issue 6439460933730304: Issue 616 - Add tests for $generichide and $genericblock (Closed)
Left Patch Set: Removed comment that I had forgotten about. Created March 19, 2015, 4:19 p.m.
Right Patch Set: Couple more tweaks Created Oct. 2, 2015, 10:37 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/content/tests/matcher.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 (function() 1 (function()
2 { 2 {
3 let server = null; 3 let server = null;
4 let frame = null; 4 let frame = null;
5 let requestNotifier = null; 5 let requestNotifier = null;
6 let httpProtocol = null; 6 let httpProtocol = null;
7 7
8 module("Content policy", { 8 module("Content policy", {
9 setup: function() 9 setup: function()
10 { 10 {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 -----END RSA PRIVATE KEY----- 58 -----END RSA PRIVATE KEY-----
59 */ 59 */
60 let publickey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl/ wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ"; 60 let publickey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl/ wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ";
61 61
62 /** 62 /**
63 * Content: 63 * Content:
64 * /test\0127.0.0.1:1234\0Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gec ko/20100101 Firefox/30.0 64 * /test\0127.0.0.1:1234\0Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gec ko/20100101 Firefox/30.0
65 */ 65 */
66 let adblockkey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl /wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ==_gM4C/j8KkD2 byPeP+THXk1GbLTUm5y+5jbdhcMtnzPMgImIfge0dGCtfU9cxLpe8BnqnEGNhTxpuu4pZxjOHYQ=="; 66 let adblockkey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl /wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ==_gM4C/j8KkD2 byPeP+THXk1GbLTUm5y+5jbdhcMtnzPMgImIfge0dGCtfU9cxLpe8BnqnEGNhTxpuu4pZxjOHYQ==";
67 67
68 let dispatchReadyEvent = "document.dispatchEvent(new CustomEvent('abp:framerea dy', {bubbles: true}));";
69
68 let tests = [ 70 let tests = [
69 [ 71 [
70 "HTML image with relative URL", 72 "HTML image with relative URL",
71 '<img src="test.gif">', 73 '<img src="test.gif">',
72 "http://127.0.0.1:1234/test.gif", "image", false, false 74 "http://127.0.0.1:1234/test.gif", "image", false, false
73 ], 75 ],
74 [ 76 [
75 "HTML image with absolute URL", 77 "HTML image with absolute URL",
76 '<img src="http://localhost:1234/test.gif">', 78 '<img src="http://localhost:1234/test.gif">',
77 "http://localhost:1234/test.gif", "image", true, false 79 "http://localhost:1234/test.gif", "image", true, false
(...skipping 17 matching lines...) Expand all
95 "Dynamically inserted image button", 97 "Dynamically inserted image button",
96 '<div id="insert"></div>' + 98 '<div id="insert"></div>' +
97 '<script>' + 99 '<script>' +
98 'window.addEventListener("DOMContentLoaded", function()' + 100 'window.addEventListener("DOMContentLoaded", function()' +
99 '{' + 101 '{' +
100 'var div = document.getElementById("insert");' + 102 'var div = document.getElementById("insert");' +
101 'div.innerHTML = \'<input type="image" id="image" src="test.gif">\';' + 103 'div.innerHTML = \'<input type="image" id="image" src="test.gif">\';' +
102 'var image = document.getElementById("image");' + 104 'var image = document.getElementById("image");' +
103 'image.onload = image.onerror = function ()' + 105 'image.onload = image.onerror = function ()' +
104 '{' + 106 '{' +
105 'document.dispatchEvent(new CustomEvent("abp:frameready", {bubbles: true}));' + 107 dispatchReadyEvent +
106 '};' + 108 '};' +
107 '}, false);' + 109 '}, false);' +
108 '</script>', 110 '</script>',
109 "http://127.0.0.1:1234/test.gif", "image", false, true 111 "http://127.0.0.1:1234/test.gif", "image", false, true
110 ], 112 ],
111 [ 113 [
112 "CSS background-image", 114 "CSS background-image",
113 '<div style="background-image: url(test.gif)"></div>', 115 '<div style="background-image: url(test.gif)"></div>',
114 "http://127.0.0.1:1234/test.gif", "image", false, false 116 "http://127.0.0.1:1234/test.gif", "image", false, false
115 ], 117 ],
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 'xmlDoc.async = false;' + 212 'xmlDoc.async = false;' +
211 'xmlDoc.load("test.xml");' + 213 'xmlDoc.load("test.xml");' +
212 '}' + 214 '}' +
213 'catch(e){}' + 215 'catch(e){}' +
214 '</script>', 216 '</script>',
215 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false 217 "http://127.0.0.1:1234/test.xml", "xmlhttprequest", false, false
216 ], 218 ],
217 [ 219 [
218 "Web worker", 220 "Web worker",
219 '<script>' + 221 '<script>' +
220 'var e = new CustomEvent(\'abp:frameready\', {bubbles: true});' +
221 'try' + 222 'try' +
222 '{' + 223 '{' +
223 'var worker = new Worker("test.js");' + 224 'var worker = new Worker("test.js");' +
224 'worker.onerror = function(event)' + 225 'worker.onerror = function(event)' +
225 '{' + 226 '{' +
226 'event.preventDefault();' + 227 'event.preventDefault();' +
227 'document.dispatchEvent(e);' + 228 dispatchReadyEvent +
228 '};' + 229 '};' +
229 '}' + 230 '}' +
230 'catch (x)' + 231 'catch (e)' +
231 '{' + 232 '{' +
232 'document.dispatchEvent(e);' + 233 dispatchReadyEvent +
233 '}' + 234 '}' +
234 '</script>', 235 '</script>',
235 "http://127.0.0.1:1234/test.js", "script", false, true 236 "http://127.0.0.1:1234/test.js", "script", false, true
236 ], 237 ],
237 ]; 238 ];
238 239
239 if (window.navigator.mimeTypes["application/x-shockwave-flash"] && window.navi gator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) 240 if (window.navigator.mimeTypes["application/x-shockwave-flash"] && window.navi gator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
240 { 241 {
241 tests.push([ 242 tests.push([
242 "HTML embed (Flash)", 243 "HTML embed (Flash)",
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 if (prevWnd == wnd && prevItem.location == item.location && prevItem.type == item.type && prevItem.docDomain == item.docDomain) 290 if (prevWnd == wnd && prevItem.location == item.location && prevItem.type == item.type && prevItem.docDomain == item.docDomain)
290 policyHits.pop(); 291 policyHits.pop();
291 } 292 }
292 policyHits.push([wnd, node, item]); 293 policyHits.push([wnd, node, item]);
293 } 294 }
294 295
295 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, e xplicitEvent], stage) 296 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, e xplicitEvent], stage)
296 { 297 {
297 defaultMatcher.clear(); 298 defaultMatcher.clear();
298 299
299 if (stage > 1) 300 if (stage == 7)
300 if (stage == 7) 301 defaultMatcher.add(Filter.fromText(expectedURL + "$domain=127.0.0.1"));
301 defaultMatcher.add(Filter.fromText(expectedURL + "$domain=127.0.0.1")); 302 else if (stage > 1)
302 else 303 defaultMatcher.add(Filter.fromText(expectedURL));
303 defaultMatcher.add(Filter.fromText(expectedURL));
304 304
305 if (stage == 3) 305 if (stage == 3)
306 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document")); 306 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document"));
307 if (stage == 4) 307 if (stage == 4)
308 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document")); 308 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document"));
309 if (stage == 5) 309 if (stage == 5)
310 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document,site key=" + publickey)); 310 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document,site key=" + publickey));
311 if (stage == 6 || stage == 7) 311 if (stage == 6 || stage == 7)
312 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$genericblock" )); 312 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$genericblock" ));
313 if (stage == 8)
314 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$genericblock, sitekey=" + publickey));
313 315
314 if (!explicitEvent) 316 if (!explicitEvent)
315 { 317 {
316 if (body.indexOf("2000/svg") >= 0) 318 if (body.indexOf("2000/svg") >= 0)
317 { 319 {
318 // SVG image: add an onload attribute to the document element 320 // SVG image: add an onload attribute to the document element and keep
319 body = body.replace(/(<svg\b)/, '$1 onload="this.dispatchEvent(new Custo mEvent(\'abp:frameready\', {bubbles: true}));"'); 321 // polling until the document is really loaded.
322 body = body.replace(/(<svg\b)/, '$1 onload="if (document.readyState != \ 'complete\') setTimeout(arguments.callee.bind(this), 0); else ' + dispatchReadyE vent + '"');
320 } 323 }
321 else 324 else
322 { 325 {
323 // HTML data: wrap it into a <body> tag 326 // HTML data: wrap it into a <body> tag
324 body = '<body onload="this.dispatchEvent(new CustomEvent(\'abp:frameread y\', {bubbles: true}));">' + body + '</body>'; 327 body = '<body onload="' + dispatchReadyEvent + '">' + body + '</body>';
325 } 328 }
326 } 329 }
327 330
328 let serverHit = false; 331 let serverHit = false;
329 server.registerPathHandler("/test", function(metadata, response) 332 server.registerPathHandler("/test", function(metadata, response)
330 { 333 {
331 response.setStatusLine("1.1", "200", "OK"); 334 response.setStatusLine("1.1", "200", "OK");
332 335
333 let contentType = "text/html"; 336 let contentType = "text/html";
334 if (body.indexOf("2000/svg") >= 0) 337 if (body.indexOf("2000/svg") >= 0)
(...skipping 25 matching lines...) Expand all
360 363
361 // Work around weird Firefox behavior, where worker scripts succesfully lo ad with empty 404 pages. 364 // Work around weird Firefox behavior, where worker scripts succesfully lo ad with empty 404 pages.
362 var error = "<b>Not found...<b>"; 365 var error = "<b>Not found...<b>";
363 response.bodyOutputStream.write(error, error.length); 366 response.bodyOutputStream.write(error, error.length);
364 }); 367 });
365 368
366 policyHits = []; 369 policyHits = [];
367 var callback = function() 370 var callback = function()
368 { 371 {
369 let expectedStatus = "allowed"; 372 let expectedStatus = "allowed";
370 if (stage == 3 || stage == 6) 373 if (stage == 3)
371 equal(policyHits.length, 0, "Number of policy hits"); 374 equal(policyHits.length, 0, "Number of policy hits");
372 // We cannot rely on the correctness of policy hits for sitekey filters du e to blocking 375 // We cannot rely on the correctness of policy hits for sitekey filters du e to blocking
373 // filter hits being counted even if the resource doesn't end up getting b locked 376 // filter hits being counted even if the resource doesn't end up getting b locked
374 else if (stage != 5) 377 else if (stage != 5 && stage != 6 && stage != 8)
375 { 378 {
376 equal(policyHits.length, 1, "Number of policy hits"); 379 equal(policyHits.length, 1, "Number of policy hits");
377 if (policyHits.length == 1) 380 if (policyHits.length == 1)
378 { 381 {
379 let [wnd, node, item] = policyHits[0]; 382 let [wnd, node, item] = policyHits[0];
380 383
381 equal(item.location, expectedURL, "Request URL"); 384 equal(item.location, expectedURL, "Request URL");
382 385
383 expectedStatus = (stage == 1 ? "allowed" : "blocked"); 386 expectedStatus = (stage == 1 ? "allowed" : "blocked");
384 let actualStatus = (item.filter ? "blocked" : "allowed"); 387 let actualStatus = (item.filter ? "blocked" : "allowed");
(...skipping 15 matching lines...) Expand all
400 frame.setAttribute("src", "http://127.0.0.1:1234/test"); 403 frame.setAttribute("src", "http://127.0.0.1:1234/test");
401 } 404 }
402 405
403 let stageDescriptions = { 406 let stageDescriptions = {
404 1: "running without filters", 407 1: "running without filters",
405 2: "running with filter %S", 408 2: "running with filter %S",
406 3: "running with filter %S and site exception", 409 3: "running with filter %S and site exception",
407 4: "running with filter %S and exception not applicable to sites", 410 4: "running with filter %S and exception not applicable to sites",
408 5: "running with filter %S and sitekey exception", 411 5: "running with filter %S and sitekey exception",
409 6: "running with filter %S and $genericblock exception", 412 6: "running with filter %S and $genericblock exception",
410 7: "running with filter %S$domain=127.0.0.1 and $genericblock exception" 413 7: "running with filter %S$domain=127.0.0.1 and $genericblock exception",
414 8: "running with filter %S and $genericblock,sitekey exception"
411 }; 415 };
412 416
413 for (let test = 0; test < tests.length; test++) 417 for (let test = 0; test < tests.length; test++)
414 { 418 {
415 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar ty] = tests[test]; 419 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar ty] = tests[test];
416 for (let stage = 1; stage in stageDescriptions; stage++) 420 for (let stage = 1; stage in stageDescriptions; stage++)
417 { 421 {
418 let stageDescription = stageDescriptions[stage]; 422 let stageDescription = stageDescriptions[stage];
419 if (stageDescription.indexOf("%S") >= 0) 423 if (stageDescription.indexOf("%S") >= 0)
420 stageDescription = stageDescription.replace("%S", expectedURL); 424 stageDescription = stageDescription.replace("%S", expectedURL);
421 425
422 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t est], stage)); 426 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t est], stage));
423 } 427 }
424 } 428 }
425 })(); 429 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld