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

Side by Side Diff: test/filterClasses.js

Issue 29680689: [$csp2 adblockpluscore] Issue 6329 - Add the CSP filter type (Closed)
Patch Set: Rebased, brought back Filter.normalize, other changes Created March 6, 2018, 2:44 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
« no previous file with comments | « lib/filterClasses.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 26 matching lines...) Expand all
37 exports.setUp = function(callback) 37 exports.setUp = function(callback)
38 { 38 {
39 let sandboxedRequire = createSandbox(); 39 let sandboxedRequire = createSandbox();
40 ( 40 (
41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
43 ElemHideException, 43 ElemHideException,
44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses") 44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses")
45 ); 45 );
46 t = RegExpFilter.typeMap; 46 t = RegExpFilter.typeMap;
47 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | 47 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP |
48 t.GENERICHIDE | t.GENERICBLOCK); 48 t.GENERICHIDE | t.GENERICBLOCK);
49 49
50 callback(); 50 callback();
51 }; 51 };
52 52
53 function serializeFilter(filter) 53 function serializeFilter(filter)
54 { 54 {
55 // Filter serialization only writes out essential properties, need to do a ful l serialization here 55 // Filter serialization only writes out essential properties, need to do a ful l serialization here
56 let result = []; 56 let result = [];
57 result.push("text=" + filter.text); 57 result.push("text=" + filter.text);
(...skipping 28 matching lines...) Expand all
86 result.push("matchCase=" + filter.matchCase); 86 result.push("matchCase=" + filter.matchCase);
87 87
88 let sitekeys = filter.sitekeys || []; 88 let sitekeys = filter.sitekeys || [];
89 result.push("sitekeys=" + sitekeys.slice().sort().join("|")); 89 result.push("sitekeys=" + sitekeys.slice().sort().join("|"));
90 90
91 result.push("thirdParty=" + filter.thirdParty); 91 result.push("thirdParty=" + filter.thirdParty);
92 if (filter instanceof BlockingFilter) 92 if (filter instanceof BlockingFilter)
93 { 93 {
94 result.push("type=filterlist"); 94 result.push("type=filterlist");
95 result.push("collapse=" + filter.collapse); 95 result.push("collapse=" + filter.collapse);
96 result.push("csp=" + filter.csp);
96 } 97 }
97 else if (filter instanceof WhitelistFilter) 98 else if (filter instanceof WhitelistFilter)
98 result.push("type=whitelist"); 99 result.push("type=whitelist");
99 } 100 }
100 else if (filter instanceof ElemHideBase) 101 else if (filter instanceof ElemHideBase)
101 { 102 {
102 if (filter instanceof ElemHideFilter) 103 if (filter instanceof ElemHideFilter)
103 result.push("type=elemhide"); 104 result.push("type=elemhide");
104 else if (filter instanceof ElemHideException) 105 else if (filter instanceof ElemHideException)
105 result.push("type=elemhideexception"); 106 result.push("type=elemhideexception");
(...skipping 28 matching lines...) Expand all
134 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || 135 if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
135 type == "elemhideexception" || type == "elemhideemulation") 136 type == "elemhideexception" || type == "elemhideemulation")
136 { 137 {
137 addProperty("disabled", "false"); 138 addProperty("disabled", "false");
138 addProperty("lastHit", "0"); 139 addProperty("lastHit", "0");
139 addProperty("hitCount", "0"); 140 addProperty("hitCount", "0");
140 } 141 }
141 if (type == "whitelist" || type == "filterlist") 142 if (type == "whitelist" || type == "filterlist")
142 { 143 {
143 addProperty("contentType", 0x7FFFFFFF & ~( 144 addProperty("contentType", 0x7FFFFFFF & ~(
144 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 145 t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK
145 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
146 RegExpFilter.typeMap.GENERICBLOCK
147 )); 146 ));
148 addProperty("matchCase", "false"); 147 addProperty("matchCase", "false");
149 addProperty("thirdParty", "null"); 148 addProperty("thirdParty", "null");
150 addProperty("domains", ""); 149 addProperty("domains", "");
151 addProperty("sitekeys", ""); 150 addProperty("sitekeys", "");
152 } 151 }
153 if (type == "filterlist") 152 if (type == "filterlist")
153 {
154 addProperty("collapse", "null"); 154 addProperty("collapse", "null");
155 addProperty("csp", "null");
156 }
155 if (type == "elemhide" || type == "elemhideexception" || 157 if (type == "elemhide" || type == "elemhideexception" ||
156 type == "elemhideemulation") 158 type == "elemhideemulation")
157 { 159 {
158 addProperty("selectorDomain", ""); 160 addProperty("selectorDomain", "");
159 addProperty("domains", ""); 161 addProperty("domains", "");
160 } 162 }
161 } 163 }
162 164
163 function compareFilter(test, text, expected, postInit) 165 function compareFilter(test, text, expected, postInit)
164 { 166 {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d /", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]); 279 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d /", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]);
278 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*" , "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]); 280 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*" , "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]);
279 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d* *dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]); 281 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d* *dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]);
280 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]); 282 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]);
281 283
282 test.done(); 284 test.done();
283 }; 285 };
284 286
285 exports.testFilterOptions = function(test) 287 exports.testFilterOptions = function(test)
286 { 288 {
287 compareFilter(test, "bla$match-case,script,other,third-party,domain=foo.com,si tekey=foo", ["type=filterlist", "text=bla$match-case,script,other,third-party,do main=foo.com,sitekey=foo", "regexp=bla", "matchCase=true", "contentType=" + (t.S CRIPT | t.OTHER), "thirdParty=true", "domains=FOO.COM", "sitekeys=FOO"]); 289 compareFilter(test, "bla$match-case,csp=first csp,script,other,third-party,dom ain=foo.com,sitekey=foo,csp=second csp", ["type=filterlist", "text=bla$match-cas e,csp=first csp,script,other,third-party,domain=foo.com,sitekey=foo,csp=second c sp", "regexp=bla", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CS P), "thirdParty=true", "domains=FOO.COM", "sitekeys=FOO", "csp=first csp; second csp"]);
288 compareFilter(test, "bla$~match-case,~script,~other,~third-party,domain=~bar.c om", ["type=filterlist", "text=bla$~match-case,~script,~other,~third-party,domai n=~bar.com", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER )), "thirdParty=false", "domains=~BAR.COM"]); 290 compareFilter(test, "bla$~match-case,~csp=csp,~script,~other,~third-party,doma in=~bar.com", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other, ~third-party,domain=~bar.com", "regexp=bla", "contentType=" + (defaultTypes & ~( t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~BAR.COM"]);
291 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com| bar.com|~bar.foo.com|~foo.bar.com,csp=c s p,sitekey=foo|bar", ["type=whitelist", "text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo .com|~foo.bar.com,csp=c s p,sitekey=foo|bar", "regexp=bla", "matchCase=true", "c ontentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=BAR.COM |FOO.COM|~BAR.FOO.COM|~FOO.BAR.COM", "sitekeys=BAR|FOO"]);
289 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com| bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@b la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo. bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC RIPT | t.OTHER), "thirdParty=true", "domains=BAR.COM|FOO.COM|~BAR.FOO.COM|~FOO.B AR.COM", "sitekeys=BAR|FOO"]); 292 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com| bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@b la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo. bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC RIPT | t.OTHER), "thirdParty=true", "domains=BAR.COM|FOO.COM|~BAR.FOO.COM|~FOO.B AR.COM", "sitekeys=BAR|FOO"]);
290 293
291 // background and image should be the same for backwards compatibility 294 // background and image should be the same for backwards compatibility
292 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp =bla", "contentType=" + (t.IMAGE)]); 295 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp =bla", "contentType=" + (t.IMAGE)]);
293 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background ", "regexp=bla", "contentType=" + (t.IMAGE)]); 296 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background ", "regexp=bla", "contentType=" + (t.IMAGE)]);
294 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); 297 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
295 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); 298 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
296 299
297 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]); 300 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]);
298 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@ http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]); 301 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@ http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
299 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@ |ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]); 302 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@ |ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
300 compareFilter(test, "@@bla$~script,~other,document", ["type=whitelist", "text= @@bla$~script,~other,document", "regexp=bla", "contentType=" + (defaultTypes & ~ (t.SCRIPT | t.OTHER) | t.DOCUMENT)]); 303 compareFilter(test, "@@bla$~script,~other,document", ["type=whitelist", "text= @@bla$~script,~other,document", "regexp=bla", "contentType=" + (defaultTypes & ~ (t.SCRIPT | t.OTHER) | t.DOCUMENT)]);
301 compareFilter(test, "@@bla$~script,~other,~document", ["type=whitelist", "text =@@bla$~script,~other,~document", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]); 304 compareFilter(test, "@@bla$~script,~other,~document", ["type=whitelist", "text =@@bla$~script,~other,~document", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
302 compareFilter(test, "@@bla$document", ["type=whitelist", "text=@@bla$document" , "regexp=bla", "contentType=" + t.DOCUMENT]); 305 compareFilter(test, "@@bla$document", ["type=whitelist", "text=@@bla$document" , "regexp=bla", "contentType=" + t.DOCUMENT]);
303 compareFilter(test, "@@bla$~script,~other,elemhide", ["type=whitelist", "text= @@bla$~script,~other,elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~ (t.SCRIPT | t.OTHER) | t.ELEMHIDE)]); 306 compareFilter(test, "@@bla$~script,~other,elemhide", ["type=whitelist", "text= @@bla$~script,~other,elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~ (t.SCRIPT | t.OTHER) | t.ELEMHIDE)]);
304 compareFilter(test, "@@bla$~script,~other,~elemhide", ["type=whitelist", "text =@@bla$~script,~other,~elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]); 307 compareFilter(test, "@@bla$~script,~other,~elemhide", ["type=whitelist", "text =@@bla$~script,~other,~elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
305 compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide" , "regexp=bla", "contentType=" + t.ELEMHIDE]); 308 compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide" , "regexp=bla", "contentType=" + t.ELEMHIDE]);
306 309
307 compareFilter(test, "@@bla$~script,~other,donottrack", ["type=invalid", "text= @@bla$~script,~other,donottrack", "reason=filter_unknown_option"]); 310 compareFilter(test, "@@bla$~script,~other,donottrack", ["type=invalid", "text= @@bla$~script,~other,donottrack", "reason=filter_unknown_option"]);
308 compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text =@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]); 311 compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text =@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]);
309 compareFilter(test, "@@bla$donottrack", ["type=invalid", "text=@@bla$donottrac k", "reason=filter_unknown_option"]); 312 compareFilter(test, "@@bla$donottrack", ["type=invalid", "text=@@bla$donottrac k", "reason=filter_unknown_option"]);
310 compareFilter(test, "@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "rea son=filter_unknown_option"]); 313 compareFilter(test, "@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "rea son=filter_unknown_option"]);
311 compareFilter(test, "@@bla$image,foobar", ["type=invalid", "text=@@bla$image,f oobar", "reason=filter_unknown_option"]); 314 compareFilter(test, "@@bla$image,foobar", ["type=invalid", "text=@@bla$image,f oobar", "reason=filter_unknown_option"]);
312 compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar, image", "reason=filter_unknown_option"]); 315 compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar, image", "reason=filter_unknown_option"]);
313 316
317 compareFilter(test, "bla$csp=report-uri", ["type=invalid", "text=bla$csp=repor t-uri", "reason=filter_invalid_csp"]);
318 compareFilter(test, "bla$csp=foo,csp=report-uri", ["type=invalid", "text=bla$c sp=foo,csp=report-uri", "reason=filter_invalid_csp"]);
319
314 test.done(); 320 test.done();
315 }; 321 };
316 322
317 exports.testElementHidingRules = function(test) 323 exports.testElementHidingRules = function(test)
318 { 324 {
319 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]); 325 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]);
320 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]); 326 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
321 compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "selectorDo main=foo", "selector=ddd", "domains=FOO"]); 327 compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "selectorDo main=foo", "selector=ddd", "domains=FOO"]);
322 compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "se lectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 328 compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "se lectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
323 compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", " selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 329 compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", " selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [ 405 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [
400 "type=elemhideemulation", 406 "type=elemhideemulation",
401 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", 407 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)",
402 "selectorDomain=foo.com", 408 "selectorDomain=foo.com",
403 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)", 409 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)",
404 "domains=FOO.COM" 410 "domains=FOO.COM"
405 ] 411 ]
406 ); 412 );
407 test.done(); 413 test.done();
408 }; 414 };
415
416 exports.testFilterNormalization = function(test)
417 {
418 // Line breaks etc
419 test.equal(Filter.normalize("\n\t\nad\ns"),
420 "ads");
421
422 // Comment filters
423 test.equal(Filter.normalize(" ! fo o## bar "),
424 "! fo o## bar");
425
426 // Element hiding filters
427 test.equal(Filter.normalize(" domain.c om## # sele ctor "),
428 "domain.com### sele ctor");
429
430 // Regular filters
431 let normalized = Filter.normalize(
432 " bl a$sitekey= foo ,domain= do main.com |foo .com,csp= c s p "
433 );
434 test.equal(
435 normalized,
436 "bl a$sitekey= foo ,domain= do main.com |foo .com,csp= c s p"
437 );
438 compareFilter(
439 test, normalized, [
440 "type=filterlist",
441 "text=" + normalized,
442 "csp=c s p",
443 "domains=DOMAIN.COM|FOO.COM",
444 "sitekeys=FOO",
445 "regexp=bla",
446 "contentType=" + t.CSP
447 ]
448 );
449
450 test.done();
451 };
OLDNEW
« no previous file with comments | « lib/filterClasses.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld