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: Created Jan. 26, 2018, 5:41 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
« lib/filterClasses.js ('K') | « 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 RegExpFilter.typeMap.CSP | RegExpFilter.typeMap.DOCUMENT |
145 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 146 RegExpFilter.typeMap.ELEMHIDE | RegExpFilter.typeMap.POPUP |
146 RegExpFilter.typeMap.GENERICBLOCK 147 RegExpFilter.typeMap.GENERICHIDE | RegExpFilter.typeMap.GENERICBLOCK
147 )); 148 ));
148 addProperty("matchCase", "false"); 149 addProperty("matchCase", "false");
149 addProperty("thirdParty", "null"); 150 addProperty("thirdParty", "null");
150 addProperty("domains", ""); 151 addProperty("domains", "");
151 addProperty("sitekeys", ""); 152 addProperty("sitekeys", "");
152 } 153 }
153 if (type == "filterlist") 154 if (type == "filterlist")
155 {
154 addProperty("collapse", "null"); 156 addProperty("collapse", "null");
157 addProperty("csp", "null");
158 }
155 if (type == "elemhide" || type == "elemhideexception" || 159 if (type == "elemhide" || type == "elemhideexception" ||
156 type == "elemhideemulation") 160 type == "elemhideemulation")
157 { 161 {
158 addProperty("selectorDomain", ""); 162 addProperty("selectorDomain", "");
159 addProperty("domains", ""); 163 addProperty("domains", "");
160 } 164 }
161 } 165 }
162 166
163 function compareFilter(test, text, expected, postInit) 167 function compareFilter(test, text, expected, postInit)
164 { 168 {
(...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]); 281 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]); 282 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]); 283 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]); 284 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]);
281 285
282 test.done(); 286 test.done();
283 }; 287 };
284 288
285 exports.testFilterOptions = function(test) 289 exports.testFilterOptions = function(test)
286 { 290 {
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"]); 291 compareFilter(test, "bla$match-case,script,other,third-party,domain=foo.com,si tekey=foo,csp= c s p ", ["type=filterlist", "text=bla$match-case,script,other,th ird-party,domain=foo.com,sitekey=foo,csp=c s p", "regexp=bla", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=FOO. COM", "sitekeys=FOO", "csp=c s p"]);
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"]); 292 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"]);
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"]); 293 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.f oo.com|~foo.bar.com,csp=c s p,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=BAR.C OM|FOO.COM|~BAR.FOO.COM|~FOO.BAR.COM", "sitekeys=BAR|FOO"]);
290 294
291 // background and image should be the same for backwards compatibility 295 // 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)]); 296 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)]); 297 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)]); 298 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)]); 299 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
296 300
297 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]); 301 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))]); 302 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))]); 303 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)]); 304 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))]); 305 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]); 306 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)]); 307 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))]); 308 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]); 309 compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide" , "regexp=bla", "contentType=" + t.ELEMHIDE]);
306 310
307 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"]);
308 compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text =@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]); 312 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"]); 313 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"]); 314 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"]); 315 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"]); 316 compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar, image", "reason=filter_unknown_option"]);
313 317
318 compareFilter(test, "bla$csp=report-uri", ["type=invalid", "text=bla$csp=repor t-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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 compareFilter(test, Filter.stripJunk(" ! fo o## bar "), [ 425 compareFilter(test, Filter.stripJunk(" ! fo o## bar "), [
420 "type=comment", "text=! fo o## bar" 426 "type=comment", "text=! fo o## bar"
421 ]); 427 ]);
422 compareFilter(test, Filter.stripJunk(" bl a$sitekey= foo ,domain= do ma in.com |foo .com "), [ 428 compareFilter(test, Filter.stripJunk(" bl a$sitekey= foo ,domain= do ma in.com |foo .com "), [
423 "type=filterlist", 429 "type=filterlist",
424 "text=bla$sitekey=foo,domain=domain.com|foo.com", 430 "text=bla$sitekey=foo,domain=domain.com|foo.com",
425 "regexp=bla", 431 "regexp=bla",
426 "sitekeys=FOO", 432 "sitekeys=FOO",
427 "domains=DOMAIN.COM|FOO.COM" 433 "domains=DOMAIN.COM|FOO.COM"
428 ]); 434 ]);
429 compareFilter(test, Filter.stripJunk(" @@bl » a$foo= bar ,domain = ab cd "), [ 435 compareFilter(test, Filter.stripJunk(" @@bl » a$foo= bar ,domain = ab cd ,foo"), [
430 "type=invalid", 436 "type=invalid",
431 "text=@@bla$invalid=foo,domain=abcd", 437 "text=@@bla$foo=bar,domain=abcd,foo",
432 "reason=filter_unknown_option" 438 "reason=filter_unknown_option"
433 ]); 439 ]);
434 440
441 compareFilter(test, "bla$domain= a b ,csp= the c s p ", [
442 "type=filterlist",
443 "contentType=" + t.CSP,
444 "regexp=bla",
445 "text=bla$domain=ab,csp=the c s p",
446 "csp=the c s p",
447 "domains=AB"
448 ]);
449 compareFilter(test, Filter.stripJunk("bla$csp= the c s p ,invalid= f o o " ), [
450 "type=invalid",
451 "text=bla$csp=the c s p,invalid=foo",
452 "reason=filter_unknown_option"
453 ]);
454
435 test.done(); 455 test.done();
436 }; 456 };
OLDNEW
« lib/filterClasses.js ('K') | « lib/filterClasses.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld