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