| Index: test/matcher.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/test/matcher.js |
| @@ -0,0 +1,160 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-present eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +"use strict"; |
|
Wladimir Palant
2017/10/17 12:51:22
This test has been taken from the master branch an
|
| + |
| +const {createSandbox} = require("./_common"); |
| + |
| +let Filter = null; |
| +let RegExpFilter = null; |
| +let Matcher = null; |
| +let defaultMatcher = null; |
| + |
| +exports.setUp = function(callback) |
| +{ |
| + let sandboxedRequire = createSandbox(); |
| + ( |
| + {Filter, RegExpFilter} = sandboxedRequire("../lib/filterClasses"), |
| + {defaultMatcher, Matcher} = sandboxedRequire("../lib/matcher") |
|
sergei
2017/10/18 10:58:44
What about deletion of defaultMatcher in tearDown?
|
| + ); |
| + |
| + callback(); |
| +}; |
| + |
| +function checkMatch(test, filters, location, contentType, docDomain, thirdParty, sitekey, specificOnly, expected) |
| +{ |
| + for (let prefix of ["", "@@"]) |
| + { |
| + // Generic whitelisting rules can match for specificOnly searches, so we |
| + // can't easily know which rule will match for these whitelisting tests |
| + if (prefix == "@@" && specificOnly) |
| + continue; |
| + |
| + let matcher = Matcher.create(); |
|
sergei
2017/10/18 10:58:43
matcher is not deleted.
|
| + let actualFilters = []; |
| + for (let text of filters) |
| + { |
| + if (prefix && !text.startsWith(prefix)) |
| + text = prefix + text; |
| + actualFilters.push(text); |
| + |
| + let filter = Filter.fromText(text); |
| + matcher.add(filter); |
| + filter.delete(); |
| + } |
| + |
| + let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], docDomain, thirdParty, sitekey, specificOnly); |
| + if (result) |
| + { |
| + let tmp = result.text; |
| + result.delete(); |
| + result = tmp; |
| + } |
| + |
| + let actualExpected = expected; |
| + if (actualExpected && prefix && !expected.startsWith(prefix)) |
| + actualExpected = prefix + actualExpected; |
| + test.equal(result, actualExpected, |
| + `match(${location}, ${contentType}, ${docDomain}, |
| + ${thirdParty ? "third-party" : "first-party"}, |
| + ${sitekey || "no-sitekey"}, |
| + ${specificOnly ? "specificOnly" : "not-specificOnly"}) |
| + with: ${actualFilters.join(" ")}`.replace(/\s+/g, " ")); |
| + } |
| +} |
| + |
| +exports.testMatcherClassDefinitions = function(test) |
| +{ |
| + test.equal(typeof Matcher, "function", "typeof Matcher"); |
| + test.equal(typeof defaultMatcher, "object", "typeof defaultMatcher"); |
| + test.ok(defaultMatcher instanceof Matcher, "defaultMatcher is a Matcher instance"); |
| + |
| + test.done(); |
| +}; |
| + |
| +exports.testFilterMatching = function(test) |
| +{ |
| + checkMatch(test, [], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc"); |
| + checkMatch(test, ["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, false, "abc"); |
| + checkMatch(test, ["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, false, "abc"); |
| + checkMatch(test, ["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["://abc/d"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/d"); |
| + checkMatch(test, ["|http://"], "http://abc/def", "IMAGE", null, false, null, false, "|http://"); |
| + checkMatch(test, ["|http://abc"], "http://abc/def", "IMAGE", null, false, null, false, "|http://abc"); |
| + checkMatch(test, ["|abc"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["|/abc/def"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["/def|"], "http://abc/def", "IMAGE", null, false, null, false, "/def|"); |
| + checkMatch(test, ["/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "/abc/def|"); |
| + checkMatch(test, ["/abc/|"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["http://abc/|"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["|http://abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, "|http://abc/def|"); |
| + checkMatch(test, ["|/abc/def|"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["|http://abc/|"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["|/abc/|"], "http://abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["||example.com/abc"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||example.com/abc"); |
| + checkMatch(test, ["||com/abc/def"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||com/abc/def"); |
| + checkMatch(test, ["||com/abc"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||com/abc"); |
| + checkMatch(test, ["||mple.com/abc"], "http://example.com/abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["||.com/abc/def"], "http://example.com/abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["||http://example.com/"], "http://example.com/abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["||example.com/abc/def|"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||example.com/abc/def|"); |
| + checkMatch(test, ["||com/abc/def|"], "http://example.com/abc/def", "IMAGE", null, false, null, false, "||com/abc/def|"); |
| + checkMatch(test, ["||example.com/abc|"], "http://example.com/abc/def", "IMAGE", null, false, null, false, null); |
| + checkMatch(test, ["://abc/d", "asdf1234"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/d"); |
| + checkMatch(test, ["foo*://abc/d", "foo*//abc/de", "://abc/de", "asdf1234"], "http://abc/def", "IMAGE", null, false, null, false, "://abc/de"); |
| + checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); |
| + checkMatch(test, ["abc$third-party", "abc$~third-party", "ddd"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); |
| + checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$~third-party"); |
| + checkMatch(test, ["//abc/def$third-party", "//abc/def$~third-party", "//abc_def"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"); |
| + checkMatch(test, ["abc$~third-party", "//abc/def"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def"); |
| + checkMatch(test, ["abc$~third-party", "//abc/def$third-party"], "http://abc/def", "IMAGE", null, true, null, false, "//abc/def$third-party"); |
| + checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$third-party"], "http://abc/def", "IMAGE", null, false, null, false, "abc$~third-party"); |
| + checkMatch(test, ["abc$third-party", "abc$~third-party", "//abc/def$~third-party"], "http://abc/def", "IMAGE", null, true, null, false, "abc$third-party"); |
| + checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "IMAGE", null, false, null, false, "abc$image"); |
| + checkMatch(test, ["abc$image", "abc$script", "abc$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "abc$script"); |
| + checkMatch(test, ["abc$image", "abc$script", "abc$~image"], "http://abc/def", "OTHER", null, false, null, false, "abc$~image"); |
| + checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); |
| + checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~script"], "http://abc/def", "SCRIPT", null, false, null, false, "//abc/def$script"); |
| + checkMatch(test, ["//abc/def$image", "//abc/def$script", "//abc/def$~image"], "http://abc/def", "OTHER", null, false, null, false, "//abc/def$~image"); |
| + checkMatch(test, ["abc$~image", "//abc/def"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def"); |
| + checkMatch(test, ["abc$~image", "//abc/def$image"], "http://abc/def", "IMAGE", null, false, null, false, "//abc/def$image"); |
| + checkMatch(test, ["abc$image", "abc$~image", "//abc/def$script"], "http://abc/def", "IMAGE", null, false, null, false, "abc$image"); |
| + 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"); |
|
Wladimir Palant
2017/10/17 12:51:22
This test and a few similar ones are currently fai
sergei
2017/11/20 16:30:49
I think it is because of mutable docDomain. docDom
|
| + 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"); |
| + 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"); |
| + 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"); |
| + 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); |
| + 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); |
| + 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"); |
| + checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey=foo-publickey"); |
| + checkMatch(test, ["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey=bar-publickey"); |
| + checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); |
| + checkMatch(test, ["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null); |
| + checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey=foo-publickey,domain=foo.com"); |
| + checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-publickey", false, null); |
| + checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-publickey", false, null); |
| + checkMatch(test, ["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-publickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey=bar-publickey,domain=bar.com"); |
| + checkMatch(test, ["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE", "foo.com", false, null, false, "@@foo.com$generichide"); |
| + checkMatch(test, ["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK", "foo.com", false, null, false, "@@foo.com$genericblock"); |
| + checkMatch(test, ["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE", "foo.com", false, null, false, null); |
| + checkMatch(test, ["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK", "foo.com", false, null, false, null); |
| + checkMatch(test, ["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, null, true, null); |
| + checkMatch(test, ["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.com", false, null, true, "/bar$domain=foo.com"); |
| + |
| + test.done(); |
| +}; |