LEFT | RIGHT |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
1 "use strict"; | 18 "use strict"; |
2 | 19 |
3 let { | 20 let {createSandbox} = require("./_common"); |
4 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 21 |
5 WhitelistFilter, ElemHideFilter, ElemHideException, CSSPropertyFilter | 22 let Filter = null; |
6 } = require("../lib/filterClasses"); | 23 let InvalidFilter = null; |
| 24 let CommentFilter = null; |
| 25 let ActiveFilter = null; |
| 26 let RegExpFilter = null; |
| 27 let BlockingFilter = null; |
| 28 let WhitelistFilter = null; |
| 29 let ElemHideBase = null; |
| 30 let ElemHideFilter = null; |
| 31 let ElemHideException = null; |
| 32 let CSSPropertyFilter = null; |
| 33 |
| 34 exports.setUp = function(callback) |
| 35 { |
| 36 let sandboxedRequire = createSandbox(); |
| 37 ( |
| 38 { |
| 39 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, |
| 40 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, |
| 41 ElemHideException, CSSPropertyFilter |
| 42 } = sandboxedRequire("../lib/filterClassesNew") |
| 43 ); |
| 44 callback(); |
| 45 }; |
7 | 46 |
8 exports.testFromText = function(test) | 47 exports.testFromText = function(test) |
9 { | 48 { |
10 let tests = [ | 49 let tests = [ |
11 ["!asdf", CommentFilter, "comment"], | 50 ["!asdf", CommentFilter, "comment"], |
12 ["asdf", RegExpFilter, "blocking"], | 51 ["asdf", BlockingFilter, "blocking"], |
13 ["asdf$image,~collapse", RegExpFilter, "blocking"], | 52 ["asdf$image,~collapse", BlockingFilter, "blocking"], |
14 ["/asdf/", RegExpFilter, "blocking"], | 53 ["/asdf/", BlockingFilter, "blocking"], |
15 ["/asdf??+/", InvalidFilter, "invalid"], | 54 ["/asdf??+/", InvalidFilter, "invalid"], |
16 ["@@asdf", WhitelistFilter, "whitelist"], | 55 ["@@asdf", WhitelistFilter, "whitelist"], |
17 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], | 56 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], |
18 ["@@/asdf/", WhitelistFilter, "whitelist"], | 57 ["@@/asdf/", WhitelistFilter, "whitelist"], |
19 ["@@/asdf??+/", InvalidFilter, "invalid"], | 58 ["@@/asdf??+/", InvalidFilter, "invalid"], |
20 ["##asdf", ElemHideFilter, "elemhide"], | 59 ["##asdf", ElemHideFilter, "elemhide"], |
21 ["#@#asdf", ElemHideException, "elemhideexception"], | 60 ["#@#asdf", ElemHideException, "elemhideexception"], |
22 ["foobar##asdf", ElemHideFilter, "elemhide"], | 61 ["foobar##asdf", ElemHideFilter, "elemhide"], |
23 ["foobar#@#asdf", ElemHideException, "elemhideexception"], | 62 ["foobar#@#asdf", ElemHideException, "elemhideexception"], |
24 ["foobar##a", ElemHideFilter, "elemhide"], | 63 ["foobar##a", ElemHideFilter, "elemhide"], |
25 ["foobar#@#a", ElemHideException, "elemhideexception"], | 64 ["foobar#@#a", ElemHideException, "elemhideexception"], |
26 | 65 |
27 ["foobar#asdf", RegExpFilter, "blocking"], | 66 ["foobar#asdf", BlockingFilter, "blocking"], |
28 ["foobar|foobas##asdf", RegExpFilter, "blocking"], | 67 ["foobar|foobas##asdf", BlockingFilter, "blocking"], |
29 ["foobar##asdf{asdf}", RegExpFilter, "blocking"], | 68 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], |
30 ["foobar##", RegExpFilter, "blocking"], | 69 ["foobar##", BlockingFilter, "blocking"], |
31 ["foobar#@#", RegExpFilter, "blocking"], | 70 ["foobar#@#", BlockingFilter, "blocking"], |
32 ["asdf$foobar", InvalidFilter, "invalid"], | 71 ["asdf$foobar", InvalidFilter, "invalid"], |
33 ["asdf$image,foobar", InvalidFilter, "invalid"], | 72 ["asdf$image,foobar", InvalidFilter, "invalid"], |
34 ["asdf$image=foobar", RegExpFilter, "blocking"], | 73 ["asdf$image=foobar", BlockingFilter, "blocking"], |
35 ["asdf$image=foobar=xyz,~collapse", RegExpFilter, "blocking"], | 74 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], |
36 | 75 |
37 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], | 76 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], |
38 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep
tion"], | 77 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep
tion"], |
39 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css
property"], | 78 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css
property"], |
40 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el
emhideexception"], | 79 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el
emhideexception"], |
41 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali
d"], | 80 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali
d"], |
42 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e
lemhideexception"], | 81 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e
lemhideexception"], |
43 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF
ilter, "invalid"], | 82 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF
ilter, "invalid"], |
44 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid
eException, "elemhideexception"], | 83 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid
eException, "elemhideexception"], |
45 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", CSSPro
pertyFilter, "cssproperty"], | 84 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", CSSPro
pertyFilter, "cssproperty"], |
(...skipping 14 matching lines...) Expand all Loading... |
60 test.equal(filter.text, text, "Correct filter text for " + text); | 99 test.equal(filter.text, text, "Correct filter text for " + text); |
61 test.ok(filter instanceof type, "Correct filter type for " + text); | 100 test.ok(filter instanceof type, "Correct filter type for " + text); |
62 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam
e); | 101 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam
e); |
63 if (type == InvalidFilter) | 102 if (type == InvalidFilter) |
64 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); | 103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
65 filter.delete(); | 104 filter.delete(); |
66 } | 105 } |
67 test.done(); | 106 test.done(); |
68 }; | 107 }; |
69 | 108 |
70 exports.getKnownFilter = function(test) | 109 exports.testClassHierarchy = function(test) |
71 { | 110 { |
72 let filter1 = Filter.getKnownFilter("someknownfilter"); | 111 let allClasses = ["Filter", "InvalidFilter", "CommentFilter", "ActiveFilter", |
73 test.ok(!filter1, "Unknown filter returns null"); | 112 "RegExpFilter", "BlockingFilter", "WhitelistFilter", "ElemHideBase", |
| 113 "ElemHideFilter", "ElemHideException", "CSSPropertyFilter"]; |
| 114 let tests = [ |
| 115 ["/asdf??+/", "Filter", "InvalidFilter"], |
| 116 ["!asdf", "Filter", "CommentFilter"], |
| 117 ["asdf", "Filter", "ActiveFilter", "RegExpFilter", "BlockingFilter"], |
| 118 ["@@asdf", "Filter", "ActiveFilter", "RegExpFilter", "WhitelistFilter"], |
| 119 ["##asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideFilter"], |
| 120 ["#@#asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideException"], |
| 121 ["example.com##[-abp-properties='something']", "Filter", "ActiveFilter", "El
emHideBase", "CSSPropertyFilter"], |
| 122 ]; |
| 123 |
| 124 for (let list of tests) |
| 125 { |
| 126 let filter = Filter.fromText(list.shift()); |
| 127 for (let cls of list) |
| 128 { |
| 129 test.ok(filter instanceof eval(cls), |
| 130 "Filter " + filter.text + " is an instance of " + cls); |
| 131 } |
| 132 |
| 133 for (let cls of allClasses) |
| 134 { |
| 135 if (list.indexOf(cls) < 0) |
| 136 { |
| 137 test.ok(!(filter instanceof eval(cls)), |
| 138 "Filter " + filter.text + " isn't an instance of " + cls); |
| 139 } |
| 140 } |
| 141 filter.delete(); |
| 142 } |
| 143 |
| 144 test.done(); |
| 145 }; |
| 146 |
| 147 exports.testGC = function(test) |
| 148 { |
| 149 let filter1 = Filter.fromText("someknownfilter"); |
| 150 test.equal(filter1.hitCount, 0, "Initial hit count"); |
| 151 |
| 152 filter1.hitCount = 432; |
74 | 153 |
75 let filter2 = Filter.fromText("someknownfilter"); | 154 let filter2 = Filter.fromText("someknownfilter"); |
76 filter1 = Filter.getKnownFilter("someknownfilter"); | 155 test.equal(filter2.hitCount, 432, "Known filter returned"); |
77 test.ok(filter1, "Known filter returned"); | 156 |
78 | 157 filter2.hitCount = 234; |
79 filter2.hitCount = 432; | 158 test.equal(filter1.hitCount, 234, "Changing second wrapper modifies original a
s well"); |
80 test.equal(filter1.hitCount, 432, "Changes on previous instance are reflected
on new instance"); | |
81 | 159 |
82 filter1.delete(); | 160 filter1.delete(); |
83 filter2.delete(); | 161 filter2.delete(); |
| 162 |
| 163 let filter3 = Filter.fromText("someknownfilter"); |
| 164 test.equal(filter3.hitCount, 0, "Filter data has been reset once previous inst
ances have been released"); |
| 165 filter3.delete(); |
84 | 166 |
85 test.done(); | 167 test.done(); |
86 }; | 168 }; |
87 | 169 |
88 exports.testNormalize = function(test) | 170 exports.testNormalize = function(test) |
89 { | 171 { |
90 let tests = [ | 172 let tests = [ |
91 [" ! comment something ", "! comment something"], | 173 [" ! comment something ", "! comment something"], |
92 [" ! \n comment something ", "! comment something"], | 174 [" ! \n comment something ", "! comment something"], |
93 [" foo bar ", "foobar"], | 175 [" foo bar ", "foobar"], |
94 [" foo , bar # # foo > bar ", "foo,bar##foo > bar", "foo,bar", "foo > bar"]
, | 176 [" foo , bar # # foo > bar ", "foo,bar##foo > bar", "foo,bar", "foo > bar"]
, |
95 [" foo , bar # @ # foo > bar ", "foo,bar#@#foo > bar", "foo,bar", "foo > b
ar"], | 177 [" foo , bar # @ # foo > bar ", "foo,bar#@#foo > bar", "foo,bar", "foo > b
ar"], |
96 ["foOBar"], | 178 ["foOBar"], |
97 ["foOBar#xyz"], | 179 ["foOBar#xyz"], |
98 ["foOBar$iMaGe,~coLLapse", "foOBar$image,~collapse"], | 180 ["foOBar$iMaGe,object_subrequest,~coLLapse", "foOBar$image,object-subrequest
,~collapse"], |
99 ["foOBar$doMain=EXample.COM|~exAMPLE.info", "foOBar$domain=example.com|~exam
ple.info"], | 181 ["foOBar$doMain=EXample.COM|~exAMPLE.РФ", "foOBar$domain=example.com|~exampl
e.рф"], |
100 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"], | 182 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"], |
101 ["exampLE.com##fooBAr", "example.com##fooBAr"], | 183 ["exampLE.com##fooBAr", "example.com##fooBAr"], |
102 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"], | 184 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"], |
| 185 ["exampLE.РФ#@#fooBAr", "example.рф#@#fooBAr"], |
103 ]; | 186 ]; |
104 | 187 |
105 for (let [text, expected, selectorDomain, selector] of tests) | 188 for (let [text, expected, selectorDomain, selector] of tests) |
106 { | 189 { |
107 if (!expected) | 190 if (!expected) |
108 expected = text; | 191 expected = text; |
109 | 192 |
110 let filter1 = Filter.fromText(text); | 193 let filter1 = Filter.fromText(text); |
111 let filter2 = Filter.fromText(expected); | 194 let filter2 = Filter.fromText(expected); |
112 | 195 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 246 |
164 // Element hiding filter | 247 // Element hiding filter |
165 filter = Filter.fromText("example.com##serialize"); | 248 filter = Filter.fromText("example.com##serialize"); |
166 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); | 249 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
167 filter.disabled = true; | 250 filter.disabled = true; |
168 filter.lastHit = 5; | 251 filter.lastHit = 5; |
169 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); | 252 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); |
170 filter.delete(); | 253 filter.delete(); |
171 | 254 |
172 test.done(); | 255 test.done(); |
173 } | 256 }; |
| 257 |
| 258 exports.testInvalidReasons = function(test) |
| 259 { |
| 260 let tests = [ |
| 261 ["/??/", "filter_invalid_regexp"], |
| 262 ["asd$foobar", "filter_unknown_option"], |
| 263 ["~foo.com##[-abp-properties='abc']", "filter_cssproperty_nodomain"], |
| 264 ]; |
| 265 |
| 266 for (let [text, reason] of tests) |
| 267 { |
| 268 let filter = Filter.fromText(text); |
| 269 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali
d"); |
| 270 filter.delete(); |
| 271 } |
| 272 |
| 273 test.done(); |
| 274 }; |
174 | 275 |
175 exports.testActiveFilter = function(test) | 276 exports.testActiveFilter = function(test) |
176 { | 277 { |
177 let filter1 = Filter.fromText("asdf"); | 278 let filter1 = Filter.fromText("asdf"); |
178 let filter1copy = Filter.fromText("asdf"); | 279 let filter1copy = Filter.fromText("asdf"); |
179 let filter2 = Filter.fromText("##foobar"); | 280 let filter2 = Filter.fromText("##foobar"); |
180 | 281 |
181 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt
ers are initially enabled"); | 282 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt
ers are initially enabled"); |
182 filter1.disabled = true; | 283 filter1.disabled = true; |
183 test.ok(filter1.disabled, "Disabling filter works"); | 284 test.ok(filter1.disabled, "Disabling filter works"); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 362 |
262 for (let [text, selector, selectorDomain] of tests) | 363 for (let [text, selector, selectorDomain] of tests) |
263 { | 364 { |
264 doTest(text, selector, selectorDomain); | 365 doTest(text, selector, selectorDomain); |
265 doTest(text.replace("##", "#@#"), selector, selectorDomain); | 366 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
266 } | 367 } |
267 | 368 |
268 test.done(); | 369 test.done(); |
269 }; | 370 }; |
270 | 371 |
271 exports.textCSSRules = function(test) | 372 exports.testCSSRules = function(test) |
272 { | 373 { |
273 let tests = [ | 374 let tests = [ |
274 ["foo.com##[-abp-properties='abc']", "abc", "", ""], | 375 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
275 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], | 376 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
276 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], | 377 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
277 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], | 378 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
278 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], | 379 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
279 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], | 380 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
280 ]; | 381 ]; |
281 | 382 |
282 for (let [text, regexp, prefix, suffix] of tests) | 383 for (let [text, regexp, prefix, suffix] of tests) |
283 { | 384 { |
284 let filter = Filter.fromText(text); | 385 let filter = Filter.fromText(text); |
285 test.equal(filter.regexpString, regexp, "Regular expression of " + text); | 386 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
286 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); | 387 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
287 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); | 388 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
288 filter.delete(); | 389 filter.delete(); |
289 } | 390 } |
290 | 391 |
291 test.done(); | 392 test.done(); |
292 }; | 393 }; |
LEFT | RIGHT |