OLD | NEW |
(Empty) | |
| 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 |
| 18 "use strict"; |
| 19 |
| 20 let {Filter, RegExpFilter} = require("filterClasses"); |
| 21 let {CombinedMatcher, defaultMatcher, Matcher} = require("matcher"); |
| 22 |
| 23 function compareKeywords(test, text, expected) |
| 24 { |
| 25 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)]) |
| 26 { |
| 27 let matcher = new Matcher(); |
| 28 let result = []; |
| 29 for (let dummy of expected) |
| 30 { |
| 31 let keyword = matcher.findKeyword(filter); |
| 32 result.push(keyword); |
| 33 if (keyword) |
| 34 { |
| 35 let dummyFilter = Filter.fromText('^' + keyword + '^'); |
| 36 dummyFilter.filterCount = Infinity; |
| 37 matcher.add(dummyFilter); |
| 38 } |
| 39 } |
| 40 |
| 41 test.equal(result.join(", "), expected.join(", "), "Keyword candidates for "
+ filter.text); |
| 42 } |
| 43 } |
| 44 |
| 45 function checkMatch(test, filters, location, contentType, docDomain, thirdParty,
sitekey, specificOnly, expected) |
| 46 { |
| 47 let matcher = new Matcher(); |
| 48 for (let filter of filters) |
| 49 matcher.add(Filter.fromText(filter)); |
| 50 |
| 51 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d
ocDomain, thirdParty, sitekey, specificOnly); |
| 52 if (result) |
| 53 result = result.text; |
| 54 |
| 55 test.equal(result, expected, "match(" + location + ", " + contentType + ", " +
docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitek
ey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly"
) + ") with:\n" + filters.join("\n")); |
| 56 |
| 57 let combinedMatcher = new CombinedMatcher(); |
| 58 for (let i = 0; i < 2; i++) |
| 59 { |
| 60 for (let filter of filters) |
| 61 combinedMatcher.add(Filter.fromText(filter)); |
| 62 |
| 63 let result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[conte
ntType], docDomain, thirdParty, sitekey, specificOnly); |
| 64 if (result) |
| 65 result = result.text; |
| 66 |
| 67 test.equal(result, expected, "combinedMatch(" + location + ", " + contentTyp
e + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ",
" + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-spe
cificOnly") + ") with:\n" + filters.join("\n")); |
| 68 |
| 69 // Generic whitelisting rules can match for specificOnly searches, so we |
| 70 // can't easily know which rule will match for these whitelisting tests |
| 71 if (specificOnly) |
| 72 continue; |
| 73 |
| 74 // For next run: add whitelisting filters for filters that aren't already |
| 75 filters = filters.map((text) => text.substr(0, 2) == "@@" ? text : "@@" + te
xt); |
| 76 if (expected && expected.substr(0, 2) != "@@") |
| 77 expected = "@@" + expected; |
| 78 } |
| 79 } |
| 80 |
| 81 function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty,
expected) |
| 82 { |
| 83 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], d
ocDomain, thirdParty); |
| 84 if (result) |
| 85 result = result.text; |
| 86 |
| 87 test.equal(result, expected, "match(" + location + ", " + contentType + ", " +
docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with stati
c filters"); |
| 88 } |
| 89 |
| 90 exports.testMatcherClassDefinitions = function(test) |
| 91 { |
| 92 test.equal(typeof Matcher, "function", "typeof Matcher"); |
| 93 test.equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher"); |
| 94 test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher"); |
| 95 test.ok(defaultMatcher instanceof CombinedMatcher, "defaultMatcher is a Combin
edMatcher instance"); |
| 96 |
| 97 test.done(); |
| 98 }; |
| 99 |
| 100 exports.testKeywordExtraction = function(test) |
| 101 { |
| 102 compareKeywords(test, "*", []); |
| 103 compareKeywords(test, "asdf", []); |
| 104 compareKeywords(test, "/asdf/", []); |
| 105 compareKeywords(test, "/asdf1234", []); |
| 106 compareKeywords(test, "/asdf/1234", ["asdf"]); |
| 107 compareKeywords(test, "/asdf/1234^", ["asdf", "1234"]); |
| 108 compareKeywords(test, "/asdf/123456^", ["123456", "asdf"]); |
| 109 compareKeywords(test, "^asdf^1234^56as^", ["asdf", "1234", "56as"]); |
| 110 compareKeywords(test, "*asdf/1234^", ["1234"]); |
| 111 compareKeywords(test, "|asdf,1234*", ["asdf"]); |
| 112 compareKeywords(test, "||domain.example^", ["example", "domain"]); |
| 113 compareKeywords(test, "&asdf=1234|", ["asdf", "1234"]); |
| 114 compareKeywords(test, "^foo%2Ebar^", ["foo%2ebar"]); |
| 115 compareKeywords(test, "^aSdF^1234", ["asdf"]); |
| 116 compareKeywords(test, "_asdf_1234_", ["asdf", "1234"]); |
| 117 compareKeywords(test, "+asdf-1234=", ["asdf", "1234"]); |
| 118 compareKeywords(test, "/123^ad2&ad&", ["123", "ad2"]); |
| 119 compareKeywords(test, "/123^ad2&ad$script,domain=example.com", ["123", "ad2"])
; |
| 120 |
| 121 test.done(); |
| 122 }; |
| 123 |
| 124 exports.testFilterMatching = function(test) |
| 125 { |
| 126 checkMatch(test, [], "http://abc/def", "IMAGE", null, false, null, false, null
); |
| 127 checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, false, null, false,
"abc"); |
| 128 checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null,
false, "abc"); |
| 129 checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null,
false, "abc"); |
| 130 checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null,
false, null); |
| 131 checkMatch(test, ["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false,
null, false, "://abc/d"); |
| 132 checkMatch(test, ["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false,
null, false, "://abc/d"); |
| 133 checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, false, null, f
alse, "|http://"); |
| 134 checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, false, null
, false, "|http://abc"); |
| 135 checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, false, null, false
, null); |
| 136 checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, false, null,
false, null); |
| 137 checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, false, null, fals
e, "/def|"); |
| 138 checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, false, null,
false, "/abc/def|"); |
| 139 checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, false, null, fal
se, null); |
| 140 checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, false, nul
l, false, null); |
| 141 checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, false,
null, false, "|http://abc/def|"); |
| 142 checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null,
false, null); |
| 143 checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, false, nu
ll, false, null); |
| 144 checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, fa
lse, null); |
| 145 checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE",
null, false, null, false, "||example.com/abc"); |
| 146 checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", nul
l, false, null, false, "||com/abc/def"); |
| 147 checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, f
alse, null, false, "||com/abc"); |
| 148 checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, null); |
| 149 checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, null); |
| 150 checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMA
GE", null, false, null, false, null); |
| 151 checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IM
AGE", null, false, null, false, "||example.com/abc/def|"); |
| 152 checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", nu
ll, false, null, false, "||com/abc/def|"); |
| 153 checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE"
, null, false, null, false, null); |
| 154 checkMatch(test, ["abc", "://abc/d", "asdf1234"], "http://abc/def", "IMAGE", n
ull, false, null, false, "://abc/d"); |
| 155 checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "h
ttp://abc/def", "IMAGE", null, false, null, false, "://abc/de"); |
| 156 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d
ef", "IMAGE", null, false, null, false, "abc$~third-party"); |
| 157 checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/d
ef", "IMAGE", null, true, null, false, "abc$third-party"); |
| 158 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de
f"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$~third-part
y"); |
| 159 checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_de
f"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"
); |
| 160 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def"], "http:/
/abc/def", "IMAGE", null, true, null, false, "//abc/def"); |
| 161 checkMatch(test, ["//abc/def", "abc$third-party", "abc$~third-party"], "http:/
/abc/def", "IMAGE", null, true, null, false, "//abc/def"); |
| 162 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part
y"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"
); |
| 163 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-part
y"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); |
| 164 checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-par
ty"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); |
| 165 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def",
"IMAGE", null, false, null, false, "abc$image"); |
| 166 checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def",
"SCRIPT", null, false, null, false, "abc$script"); |
| 167 checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def",
"OTHER", null, false, null, false, "abc$~image"); |
| 168 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"],
"http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); |
| 169 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"],
"http://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script"); |
| 170 checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"],
"http://abc/def", "OTHER", null, false, null, false, "//abc/def$~image"); |
| 171 checkMatch(test, ["abc$image", "abc$~image", "//abc/def"], "http://abc/def", "
IMAGE", null, false, null, false, "//abc/def"); |
| 172 checkMatch(test, ["//abc/def", "abc$image", "abc$~image"], "http://abc/def", "
IMAGE", null, false, null, false, "//abc/def"); |
| 173 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$image"], "http://abc/d
ef", "IMAGE", null, false, null, false, "//abc/def$image"); |
| 174 checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/
def", "IMAGE", null, false, null, false, "abc$image"); |
| 175 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$
domain=foo.com"); |
| 176 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, "abc$
domain=bar.com"); |
| 177 checkMatch(test, ["abc$domain=foo.com", "abc$domain=bar.com", "abc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, "abc$
domain=~foo.com|~bar.com"); |
| 178 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "foo.com", false, null, false, "abc$
domain=foo.com"); |
| 179 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null)
; |
| 180 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null)
; |
| 181 checkMatch(test, ["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo
.com|~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$
domain=~foo.com|~bar.com"); |
| 182 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey
=foo-publickey"); |
| 183 checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey
=bar-publickey"); |
| 184 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); |
| 185 checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "
http://abc/def", "IMAGE", "baz.com", false, null, false, null); |
| 186 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-p
ublickey", false, "abc$sitekey=foo-publickey,domain=foo.com"); |
| 187 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-p
ublickey", false, null); |
| 188 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-p
ublickey", false, null); |
| 189 checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar
-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-p
ublickey", false, "abc$sitekey=bar-publickey,domain=bar.com"); |
| 190 checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE
", "foo.com", false, null, false, "@@foo.com$generichide"); |
| 191 checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLO
CK", "foo.com", false, null, false, "@@foo.com$genericblock"); |
| 192 checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE
", "foo.com", false, null, false, null); |
| 193 checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLO
CK", "foo.com", false, null, false, null); |
| 194 checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, nu
ll, true, null); |
| 195 checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.
com", false, null, true, "/bar$domain=foo.com"); |
| 196 |
| 197 test.done(); |
| 198 }; |
| 199 |
| 200 exports.testResultCacheChecks = function(test) |
| 201 { |
| 202 let matcher = new CombinedMatcher(); |
| 203 matcher.add(Filter.fromText("abc$image")); |
| 204 matcher.add(Filter.fromText("abc$script")); |
| 205 matcher.add(Filter.fromText("abc$~image,~script,~media,~ping")); |
| 206 matcher.add(Filter.fromText("cba$third-party")); |
| 207 matcher.add(Filter.fromText("cba$~third-party,~script")); |
| 208 matcher.add(Filter.fromText("http://def$image")); |
| 209 matcher.add(Filter.fromText("http://def$script")); |
| 210 matcher.add(Filter.fromText("http://def$~image,~script,~media,~ping")); |
| 211 matcher.add(Filter.fromText("http://fed$third-party")); |
| 212 matcher.add(Filter.fromText("http://fed$~third-party,~script")); |
| 213 |
| 214 cacheCheck(test, matcher, "http://abc", "IMAGE", null, false, "abc$image"); |
| 215 cacheCheck(test, matcher, "http://abc", "SCRIPT", null, false, "abc$script"); |
| 216 cacheCheck(test, matcher, "http://abc", "OTHER", null, false, "abc$~image,~scr
ipt,~media,~ping"); |
| 217 cacheCheck(test, matcher, "http://cba", "IMAGE", null, false, "cba$~third-part
y,~script"); |
| 218 cacheCheck(test, matcher, "http://cba", "IMAGE", null, true, "cba$third-party"
); |
| 219 cacheCheck(test, matcher, "http://def", "IMAGE", null, false, "http://def$imag
e"); |
| 220 cacheCheck(test, matcher, "http://def", "SCRIPT", null, false, "http://def$scr
ipt"); |
| 221 cacheCheck(test, matcher, "http://def", "OTHER", null, false, "http://def$~ima
ge,~script,~media,~ping"); |
| 222 cacheCheck(test, matcher, "http://fed", "IMAGE", null, false, "http://fed$~thi
rd-party,~script"); |
| 223 cacheCheck(test, matcher, "http://fed", "IMAGE", null, true, "http://fed$third
-party"); |
| 224 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, false, "cba$~third-
party,~script"); |
| 225 cacheCheck(test, matcher, "http://abc_cba", "MEDIA", null, true, "cba$third-pa
rty"); |
| 226 cacheCheck(test, matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script
"); |
| 227 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, false, "http
://fed$~third-party,~script"); |
| 228 cacheCheck(test, matcher, "http://def?http://fed", "MEDIA", null, true, "http:
//fed$third-party"); |
| 229 cacheCheck(test, matcher, "http://def?http://fed", "SCRIPT", null, false, "htt
p://def$script"); |
| 230 |
| 231 test.done(); |
| 232 }; |
OLD | NEW |