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

Side by Side Diff: chrome/content/tests/policy.js

Issue 6612283790721024: Issue 698 - Added tests for $sitekey option (Closed)
Patch Set: Created Dec. 11, 2014, 2:18 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 7
7 module("Content policy", { 8 module("Content policy", {
8 setup: function() 9 setup: function()
9 { 10 {
10 prepareFilterComponents.call(this); 11 prepareFilterComponents.call(this);
11 preparePrefs.call(this); 12 preparePrefs.call(this);
12 13
13 server = new nsHttpServer(); 14 server = new nsHttpServer();
14 server.start(1234); 15 server.start(1234);
15 16
16 frame = document.createElement("iframe"); 17 frame = document.createElement("iframe");
17 frame.style.visibility = "collapse"; 18 frame.style.visibility = "collapse";
18 document.body.appendChild(frame); 19 document.body.appendChild(frame);
19 20
20 requestNotifier = new RequestNotifier(window, onPolicyHit); 21 requestNotifier = new RequestNotifier(window, onPolicyHit);
22
23 httpProtocol = Utils.httpProtocol;
24 Utils.httpProtocol = {userAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0"};
21 }, 25 },
22 teardown: function() 26 teardown: function()
23 { 27 {
24 restoreFilterComponents.call(this); 28 restoreFilterComponents.call(this);
25 restorePrefs.call(this); 29 restorePrefs.call(this);
26 30
27 stop(); 31 stop();
28 server.stop(function() 32 server.stop(function()
29 { 33 {
30 frame.parentElement.removeChild(frame); 34 frame.parentElement.removeChild(frame);
31 requestNotifier.shutdown(); 35 requestNotifier.shutdown();
32 36
33 server = null; 37 server = null;
34 frame = null; 38 frame = null;
35 requestNotifier = null; 39 requestNotifier = null;
36 40
37 start(); 41 start();
38 }); 42 });
43
44 Utils.httpProtocol = httpProtocol;
39 } 45 }
40 }); 46 });
41 47
48 /*
49 -----BEGIN RSA PRIVATE KEY-----
50 MIIBOQIBAAJBALZc50pEXnz9TSRozwM04rryuaXl/wgUFqV9FHq8HDlkdKvRU0hX
51 hb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQJBALRxYs5irhgAz2b6afOj
52 TcFr0PRtipckwW/IPw5euZKyvswEJt/tWDv4OdmDnRe8FSy6FG2Got3zxvaxYdc3
53 AXkCIQDfFGcytIVq3sbdF3lmhzcXf29R4Hrxg/eoByAKabxknwIhANFGSNMOGPt6
54 JRajfB9XmsltQJzbkr2sfHgjMN2FLM49AiAH6tt2yz1o+5snQawHXYkxBk7XIxZ5
55 9+sURZx3giUzlQIfXF+pxX9zh41i0ZtYLn181WxkGNjS7OY2CtF9wEoIfQIgcHuf
56 shh1qrvuKiXnD9b72PF676laKdzxzX5rX6cZZLA=
57 -----END RSA PRIVATE KEY-----
58 */
59 let publickey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl/ wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ";
60
61 /**
62 * Content:
63 * /test\0127.0.0.1:1234\0Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gec ko/20100101 Firefox/30.0
64 */
65 let adblockkey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZc50pEXnz9TSRozwM04rryuaXl /wgUFqV9FHq8HDlkdKvRU0hXhb/AKrSpCJ0NCxHtal1l/kHYlHG9e7Ev6+MCAwEAAQ==_gM4C/j8KkD2 byPeP+THXk1GbLTUm5y+5jbdhcMtnzPMgImIfge0dGCtfU9cxLpe8BnqnEGNhTxpuu4pZxjOHYQ==";
66
42 let tests = [ 67 let tests = [
43 [ 68 [
44 "HTML image with relative URL", 69 "HTML image with relative URL",
45 '<img src="test.gif">', 70 '<img src="test.gif">',
46 "http://127.0.0.1:1234/test.gif", "image", false, false 71 "http://127.0.0.1:1234/test.gif", "image", false, false
47 ], 72 ],
48 [ 73 [
49 "HTML image with absolute URL", 74 "HTML image with absolute URL",
50 '<img src="http://localhost:1234/test.gif">', 75 '<img src="http://localhost:1234/test.gif">',
51 "http://localhost:1234/test.gif", "image", true, false 76 "http://localhost:1234/test.gif", "image", true, false
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 { 235 {
211 return; 236 return;
212 } 237 }
213 if (item.filter instanceof WhitelistFilter) 238 if (item.filter instanceof WhitelistFilter)
214 return; 239 return;
215 240
216 if (policyHits.length > 0) 241 if (policyHits.length > 0)
217 { 242 {
218 // Ignore duplicate policy calls (possible due to prefetching) 243 // Ignore duplicate policy calls (possible due to prefetching)
219 let [prevWnd, prevNode, prevItem] = policyHits[policyHits.length - 1]; 244 let [prevWnd, prevNode, prevItem] = policyHits[policyHits.length - 1];
220 if (prevWnd == wnd && prevItem.location == item.location && prevItem.type == item.type && prevItem.docDomain == item.docDomain) 245 if (prevWnd == wnd && prevItem.location == item.location && prevItem.type == item.type && prevItem.docDomain == item.docDomain)
221 policyHits.pop(); 246 policyHits.pop();
222 } 247 }
223 policyHits.push([wnd, node, item]); 248 policyHits.push([wnd, node, item]);
224 } 249 }
225 250
226 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, w aitForMessage], stage) 251 function runTest([name, body, expectedURL, expectedType, expectedThirdParty, w aitForMessage], stage)
227 { 252 {
228 defaultMatcher.clear(); 253 defaultMatcher.clear();
229 254
230 if (stage > 1) 255 if (stage > 1)
231 defaultMatcher.add(Filter.fromText(expectedURL)); 256 defaultMatcher.add(Filter.fromText(expectedURL));
232 if (stage == 3) 257 if (stage == 3)
233 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document")); 258 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document"));
234 if (stage == 4) 259 if (stage == 4)
235 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document")); 260 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$~document"));
261 if (stage == 5)
262 defaultMatcher.add(Filter.fromText("@@||127.0.0.1:1234/test|$document,site key=" + publickey));
236 263
237 let serverHit = false; 264 let serverHit = false;
238 server.registerPathHandler("/test", function(metadata, response) 265 server.registerPathHandler("/test", function(metadata, response)
239 { 266 {
240 response.setStatusLine("1.1", "200", "OK"); 267 response.setStatusLine("1.1", "200", "OK");
241 268
242 let contentType = "text/html"; 269 let contentType = "text/html";
243 if (body.indexOf("2000/svg") >= 0) 270 if (body.indexOf("2000/svg") >= 0)
271 {
244 contentType = "image/svg+xml"; 272 contentType = "image/svg+xml";
273 body = body.replace(/^<svg/, "<svg data-adblockkey='" + adblockkey + "'" );
274 }
275 else
276 body = "<html data-adblockkey='" + adblockkey + "'>" + body + "</html>";
245 response.setHeader("Content-Type", contentType + "; charset=utf-8"); 277 response.setHeader("Content-Type", contentType + "; charset=utf-8");
246 278
247 response.bodyOutputStream.write(body, body.length); 279 response.bodyOutputStream.write(body, body.length);
248 }); 280 });
249 server.registerPathHandler("/redirect.gif", function(metadata, response) 281 server.registerPathHandler("/redirect.gif", function(metadata, response)
250 { 282 {
251 response.setStatusLine("1.1", "302", "Moved Temporarily"); 283 response.setStatusLine("1.1", "302", "Moved Temporarily");
252 response.setHeader("Location", "http://127.0.0.1:1234/test.gif"); 284 response.setHeader("Location", "http://127.0.0.1:1234/test.gif");
253 }); 285 });
254 server.registerPathHandler("/redirect2.gif", function(metadata, response) 286 server.registerPathHandler("/redirect2.gif", function(metadata, response)
(...skipping 11 matching lines...) Expand all
266 var error = "<b>Not found...<b>"; 298 var error = "<b>Not found...<b>";
267 response.bodyOutputStream.write(error, error.length); 299 response.bodyOutputStream.write(error, error.length);
268 }); 300 });
269 301
270 policyHits = []; 302 policyHits = [];
271 var callback = function() 303 var callback = function()
272 { 304 {
273 let expectedStatus = "allowed"; 305 let expectedStatus = "allowed";
274 if (stage == 3) 306 if (stage == 3)
275 equal(policyHits.length, 0, "Number of policy hits"); 307 equal(policyHits.length, 0, "Number of policy hits");
276 else 308 // We cannot rely on the correctness of policy hits for sitekey filters du e to blocking
309 // filter hits being counted even if the resource doesn't end up getting b locked
310 else if (stage != 5)
277 { 311 {
278 equal(policyHits.length, 1, "Number of policy hits"); 312 equal(policyHits.length, 1, "Number of policy hits");
279 if (policyHits.length == 1) 313 if (policyHits.length == 1)
280 { 314 {
281 let [wnd, node, item] = policyHits[0]; 315 let [wnd, node, item] = policyHits[0];
282 316
283 equal(item.location, expectedURL, "Request URL"); 317 equal(item.location, expectedURL, "Request URL");
284 318
285 expectedStatus = (stage == 1 ? "allowed" : "blocked"); 319 expectedStatus = (stage == 1 ? "allowed" : "blocked");
286 let actualStatus = (item.filter ? "blocked" : "allowed"); 320 let actualStatus = (item.filter ? "blocked" : "allowed");
(...skipping 18 matching lines...) Expand all
305 window.addEventListener("message", callback, false, true); 339 window.addEventListener("message", callback, false, true);
306 else 340 else
307 frame.addEventListener("load", callback, false); 341 frame.addEventListener("load", callback, false);
308 } 342 }
309 343
310 let stageDescriptions = { 344 let stageDescriptions = {
311 1: "running without filters", 345 1: "running without filters",
312 2: "running with filter %S", 346 2: "running with filter %S",
313 3: "running with filter %S and site exception", 347 3: "running with filter %S and site exception",
314 4: "running with filter %S and exception not applicable to sites", 348 4: "running with filter %S and exception not applicable to sites",
349 5: "running with filter %S and sitekey exception"
315 }; 350 };
316 351
317 for (let test = 0; test < tests.length; test++) 352 for (let test = 0; test < tests.length; test++)
318 { 353 {
319 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar ty] = tests[test]; 354 let [name, body, expectedURL, expectedType, expectedDomain, expectedThirdPar ty] = tests[test];
320 for (let stage = 1; stage in stageDescriptions; stage++) 355 for (let stage = 1; stage in stageDescriptions; stage++)
321 { 356 {
322 let stageDescription = stageDescriptions[stage]; 357 let stageDescription = stageDescriptions[stage];
323 if (stageDescription.indexOf("%S") >= 0) 358 if (stageDescription.indexOf("%S") >= 0)
324 stageDescription = stageDescription.replace("%S", expectedURL); 359 stageDescription = stageDescription.replace("%S", expectedURL);
325 360
326 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t est], stage)); 361 asyncTest(name + " (" + stageDescription + ")", runTest.bind(null, tests[t est], stage));
327 } 362 }
328 } 363 }
329 })(); 364 })();
OLDNEW

Powered by Google App Engine
This is Rietveld